I have a very short PowerShell script that connects to a server and imports the AD module. I'd like to run the script simply by double clicking, but I'm afraid the window immediately closes after the last line.
How can I sort this out?
I have a very short PowerShell script that connects to a server and imports the AD module. I'd like to run the script simply by double clicking, but I'm afraid the window immediately closes after the last line.
How can I sort this out?
You basically have 3 options to prevent the PowerShell Console window from closing, that I describe in more detail on my blog post.
PowerShell -NoExit "C:\SomeFolder\SomeScript.ps1"
Read-Host -Prompt "Press Enter to exit"
-NoExit
switch to always leave the PowerShell Console window open after the script finishes running.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\""
See my blog for more information and a script to download that will make the registry change for you.
powershell -NoExit -Command { <The script to actually run> }
Enables user to start the script via UI and see the results without editing registry keys –
Plectognath -NoExit
and runs SomeScript.ps1? –
Reisman 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. –
Reisman Errr... I should have known:
powershell -noexit <path\script>
and that's all there's to it :)
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
}
Just add pause on a new line at the bottom of the script, as in a batch file.
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).
In my own case, I wanted to add powershell to context menu on windows 7. That is right clicking on a folder or inside a folder to get a menu to launch Powershell window without it closing after launch. The answers here helped me do just that and I want to share it here incase it helps someone else.
To make the right click work inside a folder meaning right clicking an empty space inside a folder, repeat the steps but this time, the registry location is:
HKEY_CLASSES_ROOT\Directory\shell
And the command is:
C:\Windows\system32\WindowsPowerShell\v1.0\PowerShell.exe -noexit
Tested on windows 7 ultimate sp1 but I think I might work for later versions of Windows as well
While the original poster asked for PowerShell 2.0, all current answers should work up to PowerShell <6 (powershell.exe
). For modern PowerShell >= 6 (pwsh.exe
) you can merge the following registry directives as a global configuration to have the PowerShell host keeping its window open after running the script:
; Set to run .ps1 scripts with double click and pwsh and to not exit after running script
[HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\Open\Command]
@="C:\\Program Files\\PowerShell\\7\\pwsh.exe -noexit -Command \"$host.UI.RawUI.WindowTitle = 'PowerShell 7 (x64)'; & '%1'\""
; Set to run .ps1 scripts in the context menu with pwsh and to not exit after running script
[HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\PowerShell7x64\Command]
@="C:\\Program Files\\PowerShell\\7\\pwsh.exe -noexit -Command \"$host.UI.RawUI.WindowTitle = 'PowerShell 7 (x64)'; & '%1'\""
[HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\runas\Command]
@="C:\\Program Files\\PowerShell\\7\\pwsh.exe -noexit -Command \"$host.UI.RawUI.WindowTitle = 'PowerShell 7 (x64)'; & '%1'\""
It works with Windows 10 and PowerShell 7. Feel free to suggest fixes for Windows 11, as it appears there's now a global location for the PowerShell command.
© 2022 - 2025 — McMap. All rights reserved.