2

I clear variable cache in my powershell script using "remove-variable variable name" . I wanted to know is there cmd to clear the variable cache of all variables in the script in one go.

1

3 Answers 3

2

Be careful, apparently under some scenarios, you can munge things up a bit with over zealous variable clearing: Powershell ISE cannot start after (foolishly) deleting all variables

Sign up to request clarification or add additional context in comments.

Comments

1

You can add this at the top of you script, or in the profile:

$sysvars = get-variable | select -ExpandProperty name
$sysvars += 'sysvar'

And then do this at the end:

get-variable * |
 where { $_.name  -notin $sysvars } |
 Remove-Variable

You can also do this:

 get-variable -Scope script | remove-variable

but it may delete variables you didn't intent do if you're working in the ISE.

Comments

0

You can use: remove-item variable:* -force, I guess.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.