## 7 Answers 7 Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) This answer is useful 176 Save this answer. Timeline Show activity on this post. You basically have 3 options to prevent the PowerShell Console window from closing, that I describe in more detail on my blog post. 1. **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"` 2. **Per-script Fix:** Add a prompt for input to the end of your script file. e.g. `Read-Host -Prompt "Press Enter to exit"` 3. **Global Fix:** Change your registry key by adding the `-NoExit` switch to always leave the PowerShell Console window open after the script finishes running (see registry keys below). * * * Registry key: `HKEY_CLASSES_ROOT\Applications\powershell.exe\shell\open\command` - Description: Key used when you right-click a `.ps1` file and choose Open With -> Windows PowerShell. - Default value: `"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "%1"` - Desired value: `"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "& \"%1\""` Registry key: `HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\0\Command` - Description: Key used when you right-click a `.ps1` file and choose Run with PowerShell (shows up depending on which Windows OS and Updates you have installed). - Default value: `"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-Command" "if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & '%1'"` - Desired value: `"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoExit "-Command" "if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & \"%1\""` Note: The registry keys to update may change in future versions of Windows, but this shows you the basic idea. See my blog for more information and a script to download that will make the registry change for you. Share Share a link to this answer Copy link CC BY-SA 4.0 Improve this answer Follow Follow this answer to receive notifications edited Nov 17, 2024 at 19:40 answered Jul 7, 2014 at 22:14 !deadlydog's user avatar deadlydog deadlydog 24.7k1515 gold badges124124 silver badges137137 bronze badges 5 - 6 OPs answer also works as per-script fix: wrap your script with a `powershell -NoExit -Command { <The script to actually run> }` Enables user to start the script via UI and see the results without editing registry keys – SchreiberLex CommentedJan 9, 2019 at 9:45 - How can I make a shortcut that opens a PowerShell window (not cmd) with `-NoExit` and runs SomeScript.ps1? – xr280xr CommentedJun 20, 2022 at 17:00 - It looks like `start` is what I needed. Created a batch file with `start PowerShell -NoExit -File "[.ps1 script path]"`. Without `start` it was running PowerShell within the Windows Command prompt. – xr280xr CommentedJun 20, 2022 at 17:03 - Can also add this to your $profile if you just want to see the error, and still close out if the script finishes without error: `Register-EngineEvent -SourceIdentifier PowerShell.Exiting -Action { if ($LASTEXITCODE -ne 0) { Write-Host "Error occurred, press any key to close"; $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyUp") > $null } }` – IgorGanapolsky CommentedSep 2, 2022 at 16:51 - This will exit the console if the script runs with no errors, but wait if there is any error, allowing you to see it. `Register-EngineEvent -SourceIdentifier PowerShell.Exiting -Action { if ($LASTEXITCODE -ne 0) { Read-Host -Prompt "Script finished with error. Press any key to close." } } -SupportEvent` – deadlydog CommentedSep 2, 2022 at 17:06 - #3 doesn't work on Win 10 Pro v 10.0.19044 Build 19044 – Linus Proxy CommentedJun 29, 2022 at 13:43 - You could also associate the PowerShell ISE application with your script. – Rich M CommentedJan 26, 2023 at 13:35 Add a comment \| Expand to show all comments on this post This answer is useful 31 Save this answer. Timeline Show activity on this post. Errr... I should have known: ``` powershell -noexit <path\script> ``` and that's all there's to it :) Share Share a link to this answer Copy link CC BY-SA 3.0 Improve this answer Follow Follow this answer to receive notifications answered May 24, 2013 at 16:19 !JustAGuy's user avatar JustAGuy JustAGuy 6,0431414 gold badges4646 silver badges5858 bronze badges 1 - 5 @IgorGanapolsky The problem might be that you are appending -NoExit after the script path, it needs to go before. I think if it is after it passes it as an argument to the script instead of powershell.exe. – Joel McBeth CommentedOct 22, 2015 at 18:00 Add a comment \| Expand to show all comments on this post This answer is useful 6 Save this answer. Timeline Show activity on this post. The solution below prevents the script from closing when running Powershell ISE and allows the script to close otherwise. ``` # If running in the console, wait for input before closing. if ($Host.Name -eq "ConsoleHost") { Write-Host "Press any key to continue..." $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyUp") > $null } ``` Share Share a link to this answer Copy link CC BY-SA 4.0 Improve this answer Follow Follow this answer to receive notifications answered Oct 30, 2018 at 22:31 !Ibn Masood's user avatar Ibn Masood Ibn Masood 1,13311 gold badge1515 silver badges3131 bronze badges Add a comment \| Expand to show all comments on this post This answer is useful 4 Save this answer. Timeline Show activity on this post. You can add `pause` at the end of your script if you just want the window to stay open, or you can add `powershell` if you want to be able to run commands afterwards (obviously don't do the second option if anyone else will use your code). Share Share a link to this answer Copy link CC BY-SA 4.0 Improve this answer Follow Follow this answer to receive notifications answered Jan 29, 2021 at 5:12 !user7868's user avatar user7868 user7868 31611 silver badge88 bronze badges Add a comment \| Expand to show all comments on this post This answer is useful 2 Save this answer. Timeline Show activity on this post. Just add pause on a new line at the bottom of the script, as in a batch file. Share Share a link to this answer Copy link CC BY-SA 4.0 Improve this answer Follow Follow this answer to receive notifications answered Oct 22, 2020 at 20:34 !JMR's user avatar JMR JMR 2111 bronze badge 1 - This does not work for me. When running the script with the 'Pause' line at the end, PowerShell launches and I'm presented with "Press Enter to continue...:" When I press Enter, the script runs and then closes on completion as it would without the 'Pause' function. – Rich M CommentedJan 26, 2023 at 13:45 Add a comment \| Expand to show all comments on this post