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.

user3898271
  • 35
  • 2
  • 3
  • 7

3 Answers3

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

Community
  • 1
  • 1
Booga Roo
  • 1,665
  • 1
  • 21
  • 30
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
0

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

David Brabant
  • 41,623
  • 16
  • 83
  • 111