Run Selection v. Run Selection in Console
PowerShell Studio is full of features and options. As a new user, I’m trying to learn as many as I can. There are four options in the Run menu, so, of course, I tried them all. But the result was unexpected.
Run and Run Selection run your code and show the result in the Output pane. Run in Console and Run Selection in Console run your code and show the result in the Console pane, which is a separate Windows PowerShell process that PowerShell Studio hosts. That’s expected.
But Run Selection and Run Selection in Console are very different from each other.
- Run Selection runs each selection in a separate Windows PowerShell session. It’s as though you opened a PowerShell window, ran some commands, displayed the results, and immediately closed the window. If you use Run Selection to run a few more commands, you just opened a new window with a new session that is unaware of the previous session.
- Run Selection in Console uses the same Windows PowerShell session for all commands. You might have heard this called a “persistent runspace.” When you run a series of Run Selection in Console commands, all of the commands run in the same session and they’re aware of each other. If you create a session-specific object, such as a variable, it’s available to all selections that you run.
To test the difference, run the following commands as separate selections, first by using Run Selection and then by using Run Selection in Console.
- $a = “Try the zip cmdlets (-Archive) in Windows PowerShell 5.0 Preview”
If you use Run Selection, the result of the final “$a” statement is $null, because the assignment statement ran in a different session.
But if you use Run Selection in Console, the result is:
If you need a new Windows PowerShell session in the Console pane, type exit or right-click and click Restart Shell. (If you type powershell, you get a nested session, just like you would in the Windows PowerShell console. To return to the original session, type quit.)
Now that I understand these features, I can use PowerShell Studio more adeptly. And, what about those Archive cmdlets? I’ll save that treat for another post!