Getting a Clean PowerShell Environment

by Feb 12, 2015

PowerShell 3.0 and later

When you develop PowerShell scripts in the PowerShell ISE, you will probably make a lot of changes and revisions, and regularly execute scripts for testing.

This can “pollute” your environment: all variables defined in script scope remain defined, so a following test run will not run in a clean default environment anymore.

To make sure a script runs in a completely clean test environment, you could of course restart the PowerShell ISE. A more convenient way is to open another PowerShell tab: in the PowerShell ISE, choose File/New PowerShell Tab.

This adds another tab to the script pane. The tab represents a completely new PowerShell host. You can now load your test script into this tab, and test drive it there. Once done, simply close the tab with a click, and it will dispose all of its contents in memory again.

Note that you can open a script simultaneously in multiple tabs. When you do this, ISE warns you that the script is open in another tab already. When you edit the script, all edits will apply to all open instances of this script across all tabs.

Twitter This Tip! ReTweet this Tip!