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.
3 Answers
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
Comments
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.