How to Clean Up PowerShell Script Variables without Restarting PowerShell ISE

In this tutorial, we will see how to clean up variables and modules in PowerShell ISE without restart.


PowerShell ISE and Variables values

PowerShell ISE does not automatically cleanup it’s variable between manual runs of a script. Due to this, results doesn't came out as we expected.

Run below command to clear all variables stored in the session.

Remove-Variable * -ErrorAction SilentlyContinue

This will take care of clearing all stored variables so we can be sure, script will not inherit any value from a previous run.


PowerShell ISE Errors and ModulesPermalink

Issue which we face due variable retains value from previous run, can happen with Error buffer also, which can be cleared with the below command.

$error.Clear()

We can also remove any module loaded by our script with the below command

Remove-Module *

Run below command in one go to have fresh PowerShell ISE session. 

Remove-Variable * -ErrorAction SilentlyContinueRemove-Module *$error.Clear();

Comments

Popular Posts

PowerShell Arithmetic Operators

How to Import and Export Delimited Files, like CSV, in PowerShell

How to generate a GUID in PowerShell