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.
Asked
Active
Viewed 2.2k times
2
-
http://stackoverflow.com/a/17680309/520612 – CB. Feb 26 '15 at 09:13
3 Answers
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
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.

mjolinor
- 66,130
- 7
- 114
- 135