Remove all user defined variables without restarting the PowerShell Console or ISE

Recently, fellow Microsoft MVP Mickey Gousset asked me how to remove existing user defined variables from the PowerShell ISE (Integrated Scripting Environment) before running a script a second time without having to restart the ISE.

While you could keep track of the variables you’ve used within your script to remove them once the script completes with the Remove-Variable cmdlet or by deleting them from the variable PSDrive, that can be a less than desirable solution for long and complicated scripts that define lots of variables. You could also save your script as a PS1 file and run the PS1 file instead of running it interactively in the ISE. If the PS1 file is run, the variables will be defined in the script scope by default and when the script completes, the script scope is removed along with the variables as long as you haven’t coerced them into the global scope.

A better solution is to use a variable to either store a list of the default variables when PowerShell starts or before your script runs. Be sure you understand the precedence of the different PowerShell profiles since you maybe defining variables in some of your profiles that you don’t want to remove. See my blog article “PowerShell $Profile: The six options and their precedence“.

I’ll place the following code in my profile for the ISE ($profile.AllUsersCurrentHost or $profile.CurrentUserCurrentHost from within the ISE):

Not sure where the profile is or how to add this code to it? The following code can be used to automatically add the necessary items to the all users ISE profile:

This could even be taken a step further and turned unto a function to add those two lines of code to any of the available profiles. I’ve removed the check to make sure it’s run in the ISE since the same process could be used to remove user defined variables in the console.

Write a script defining whatever variables you want. Run the script interactively by pressing F5 from within the ISE. I’ll use the following code for simplicity:

My first thought was to use the Compare-Object cmdlet, retrieve a new list of all the variables, compare that list to the original, and remove the results. That’s a little more complicated than simply getting a list of variables while excluding the original ones and then simply piping the results to Remove-Variable:

I’ll turn that code into a function so I can simply call it without having to remember any syntax or the variable name:

Running the function with the Verbose parameter shows that the two previously defined variables have been successfully removed:

The functions shown in this blog article can be installed from the PowerShell Gallery as part of my MrToolkit PowerShell module (version 1.3 and higher) with the Install-Module cmdlet that is part of the PowerShellGet module. PowerShellGet exists by default on PowerShell version 5.0 and higher and can be downloaded for PowerShell version 3.0 and higher.

The functions shown in this blog article have also been added to my PowerShell repository on GitHub.


Leave a Reply

%d bloggers like this: