Stop Powershell from exiting
This script will not exit if you run it without arguments, e.g. by double-clicking on it:
param($Work)
# restart PowerShell with -noexit, the same script, and 1
if (!$Work) {
powershell -noexit -file $MyInvocation.MyCommand.Path 1
return
}
# now the script does something
# this script just outputs this:
'I am not exiting'
You basically have 3 options to prevent the PowerShell Console window from closing, that I describe in more detail on my blog post.
- One-time Fix: Run your script from the PowerShell Console, or launch the PowerShell process using the -NoExit switch. e.g.
PowerShell -NoExit "C:\SomeFolder\SomeScript.ps1"
- Per-script Fix: Add a prompt for input to the end of your script file. e.g.
Read-Host -Prompt "Press Enter to exit"
- Global Fix: Change your registry key to always leave the PowerShell Console window open after the script finishes running.
See my blog for more information on which registry keys to modify.
Sounds like you are looking for option #1 or #3.
"do stuff"
Pause
Yes, just like commandline -- output:
do stuff
Press Enter to continue...:
Have you tried
$host.enternestedprompt()
That will stop execution and drop them to a nested prompt. When they exit that prompt, then the script will finish and the window will close.