Why does the PowerShell ISE not isolate script variables to the script scope?

In your script examples you created variables and you didn't destroy them at the end of your script. The ISE creates a live instance of powershell which loads and runs the script when you click run. The difference is that the integrated shell can continue the script. This is ideal for debugging the environment and for creating scripts as you go. This way you don't have to keep running your script over and over (there are situations where this wouldn't be ideal) to make sure the next line of code worked. You type it in the shell, and if it works, you add it to the script section.

This behavior is perhaps best described here: http://technet.microsoft.com/en-us/library/dd819480.aspx

Relevant Excerpt:

All panes in ISE are always in the same scope.

If you don't want your variables to live in the shell after your script has completed then you should Remove-Variable them.

For example:

Remove-Variable x

You can add a "clean" instance of powershell to the ISE by clicking File->New Powershell Tab


In the ISE Powershell is essentially dumping the script to the shell and then running it. As if you typed it out in the shell, then executed it. So the variable is available for that session. To see other differences, check out this MSDN post.