Whenever you execute a PowerShell script it will run and do its thing, but afterwards it will close PowerShell.
Only when you already have a prompt or scripting editor open and execute the script from there, PowerShell will remain open.
There is a solution for this, specifically the -NoExit parameter to PowerShell.exe
You may want to execute a script, by rightclicking the script and select ‘Run with PowerShell’, and leave the shell open when its finished… that may be a little tricky to do 😉
When you start your script with the following, it will accomplish that task:
1 2 3 4 5 6 7 8 |
param ( $Show ) if ( !$Show ) { PowerShell -NoExit -File $MyInvocation.MyCommand.Path 1 return } # Put your code here... Write-Output 'I will not go away!' |
Thanks for that!
This was helpful, however, using this, the script cannot be run in the powershell editor, which is a bummer. Is there any fix for this?
Hi Caleb,
Correct. But I see executing scripts and executing scripts from an editor as two very different scenario’s. Keeping a script/console open is mainly used for viewing output. You aready see output when you execute this from an editor. So you wouldn’t need it, right?
Jeff.
This works perfect. Thank you for taking the time to post this.
awesome thx!