我有一个非常短的 PowerShell 脚本,用于连接到服务器并导入 AD 模块。我想通过双击来运行脚本,但恐怕窗口在最后一行后立即关闭。
我该如何解决这个问题?
您基本上有 3 个选项来阻止 PowerShell 控制台窗口关闭,我在我的博客文章中更详细地描述了这些。
PowerShell -NoExit "C:\SomeFolder\SomeScript.ps1"
Read-Host -Prompt "Press Enter to exit"
-NoExit
开关更改注册表项,以便在脚本运行完毕后始终保持 PowerShell 控制台窗口打开。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\""
请参阅我的博客以获取更多信息和下载脚本,该脚本将为您更改注册表。
呃…… 我应该知道:
powershell -noexit <path\script>
这就是全部:)
下面的解决方案可防止脚本在运行 Powershell ISE 时关闭,并允许脚本在其他情况下关闭。
# 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
}
只需在脚本底部的新行上添加暂停,就像在批处理文件中一样。
如果您只想让窗口保持打开状态,则可以在脚本末尾添加
pause
,或者如果您希望之后能够运行命令,则可以添加 powershell
(显然,如果其他人都会使用您的代码)。
就我自己而言,我想将 powershell 添加到 Windows 7 上的上下文菜单中。即右键单击文件夹或文件夹内部以获取启动 Powershell 窗口的菜单,而无需在启动后关闭它。这里的答案帮助我做到了这一点,我想在这里分享它,以防它对其他人有帮助。
要使右键单击在文件夹内起作用(即右键单击文件夹内的空白区域),请重复这些步骤,但这一次,注册表位置是:
HKEY_CLASSES_ROOT\目录\shell
命令是:
C:\Windows\system32\WindowsPowerShell 1.0\PowerShell.exe -noexit
在 Windows 7 Ultimate sp1 上进行了测试,但我想我也可能适用于更高版本的 Windows
所有答案对我来说似乎都已经过时了,指的是PowerShell <6 (
powershell.exe
),而不是现代PowerShell >= 6(pwsh.exe
)。如果合并以下行,则在将脚本作为全局配置运行后,PowerShell 主机将保持其窗口打开状态:
; 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'\""
它适用于 Windows 10 和 PowerShell 7。请随意提出针对 Windows 11 的修复建议,因为 PowerShell 命令现在似乎有一个全局位置。