When PS scripts executes, it is possibly create much user defined variables.
So, sometimes these varibales could confuse us a lot.
Here's a workaound:
Most of the variables can is found in System.Management.Automation.SpecialVariables
. If you filter out these and a small list of other known variables, you can create a reusable function to get user-defined Variables
functionget-udvariable {Get-variable | Where-object {(@( "Formatenumerationlimit", "Maximumaliascount", "Maximumdrivecount", "Maximumerrorcount", "Maximumfunctioncount", "Maximumvariablecount", "Pghome", "Pgse", "pguiculture", "pgversiontable", " Profile", "pssessionoption" ) -notcontains $_. Name)-and' ([PsObject]. Assembly.GetType (' System.Management.Automation.SpecialVariables '). GetFields (' nonpublic,static ')| Where-object FieldType-eq([string]) |ForEach-object GetValue$null))-notcontains $_. Name}}
In your script, just use:
get-udvariable | Remove-variable
One more suggestion:
I Don ' t think it'll be the best solution to solve problems made by remaining varibales.
When we are create varibale in our scripts, we should has a think what's the proper scope?
MSDN ref:https://technet.microsoft.com/en-us/library/hh847849.aspx
PowerShell Remove all user defined variable in PowerShell