Clearing All User Variables

by Jul 13, 2017

In the previous tip we illustrated how you can identify built-in PowerShell variables with an approach like this:

$ps = [PowerShell]::Create()
$null = $ps.AddScript('$null=$host;Get-Variable') 
$ps.Invoke()
$ps.Runspace.Close()
$ps.Dispose()

Now let’s do the opposite, and create a function that dumps only your own variables:

function Get-UserVariable ($Name = '*')
{
  # these variables may exist in certain environments (like ISE, or after use of foreach)
  $special = 'ps','psise','psunsupportedconsoleapplications', 'foreach', 'profile'

  $ps = [PowerShell]::Create()
  $null = $ps.AddScript('$null=$host;Get-Variable') 
  $reserved = $ps.Invoke() | 
    Select-Object -ExpandProperty Name
  $ps.Runspace.Close()
  $ps.Dispose()
  Get-Variable -Scope Global | 
    Where-Object Name -like $Name |
    Where-Object { $reserved -notcontains $_.Name } |
    Where-Object { $special -notcontains $_.Name } |
    Where-Object Name 
}

Now it’s really easy to identify all the variables you (or your scripts) have created that still linger around in memory:

 
PS> Get-UserVariable

Name                           Value                                                                         
----                           -----                                                                         
hash                           {Extensions, Link, Options, GPOLink...}                                       
prop                           lParam                                                                        
reserved                       {$, ?, ^, args...}                                                            
result                         {System.Management.Automation.PSVariable, System.Management.Automation.Ques...
varCount                       43                                                                            



PS> Get-UserVariable -Name pr*

Name                           Value                                                                         
----                           -----                                                                         
prop                           lParam                                                                        
 

To clean up your runspace, you could now delete all your variables in one line:

 
PS> Get-UserVariable

Name                           Value                                                                         
----                           -----                                                                         
hash                           {Extensions, Link, Options, GPOLink...}                                       
key                            HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\H...
prop                           lParam                                                                        
reserved                       {$, ?, ^, args...}                                                            
result                         {System.Management.Automation.PSVariable, System.Management.Automation.Ques...
varCount                       43                                                                            



PS> Get-UserVariable | Remove-Variable

PS> Get-UserVariable

PS>  
 

Twitter This Tip! ReTweet this Tip!