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
  • 1
  • 3
  • 7

3 Answers3

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
  • 62,812
  • 6
  • 108
  • 130
1

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,468
  • 1
  • 23
  • 26
0

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

David Brabant
  • 39,073
  • 16
  • 82
  • 103