如何在运行PowerShell脚本后保持Shell窗口打开?

92
我有一个非常简短的PowerShell脚本,它连接到服务器并导入AD模块。我希望通过双击运行该脚本,但我担心在最后一行之后窗口立即关闭。
如何解决这个问题?

相关(重复):*如何让PowerShell保持命令窗口打开?* - Peter Mortensen
6个回答

157

你基本上有三个选项来防止PowerShell控制台窗口关闭,我在我的博客文章中更详细地描述了它们。

  1. 一次性修复:从PowerShell控制台运行脚本,或使用-NoExit开关启动PowerShell进程。例如:PowerShell -NoExit "C:\SomeFolder\SomeScript.ps1"
  2. 每个脚本的修复:在脚本文件末尾添加输入提示。例如:Read-Host -Prompt "按Enter键退出"
  3. 全局修复:通过将-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\""

请查看我的博客获取更多信息,并下载一个脚本,它将帮助您进行注册表更改。


4
OP的答案也可以作为脚本修复:将您的脚本用powershell -NoExit -Command { <要实际运行的脚本> }包装起来。 使用户能够通过UI启动脚本并查看结果,而无需编辑注册表键。 - SchreiberLex
我该如何创建一个快捷方式,打开一个带有 -NoExit 参数的 PowerShell 窗口(而不是 cmd),并运行 SomeScript.ps1? - xr280xr
看起来 start 是我需要的。创建了一个批处理文件,其中包含 start PowerShell -NoExit -File "[.ps1 脚本路径]"。如果没有 start,它会在 Windows 命令提示符中运行 PowerShell。 - xr280xr
#3在Win 10 Pro v 10.0.19044 Build 19044上无法运行。 - Linus Proxy
您还可以将PowerShell ISE应用程序与您的脚本关联。 - Rich M

30

呃...... 我本应该知道的:

powershell -noexit <path\script> 

就是这样了 :)


5
可能的问题是您将-NoExit附加在脚本路径后面,它需要放在前面。如果在脚本路径后面添加-NoExit,它会将其作为参数传递给脚本而不是powershell.exe。 - Joel McBeth

6
下面的解决方案可防止脚本在运行 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
}

2

如果你只想让窗口保持打开状态,可以在脚本末尾添加pause;如果你想要在之后运行命令,可以添加powershell(如果其他人也会使用你的代码,显然不要选择第二个选项)。


2
只需要在脚本底部添加一个新行,并输入“pause”,就像批处理文件一样。

这对我不起作用。当在脚本末尾使用“Pause”行运行脚本时,PowerShell会启动并显示“按Enter键继续...:”。 当我按下Enter键时,脚本运行,然后在完成时关闭,就像没有“Pause”函数一样。 - Rich M

1
在我的情况下,我想在Windows 7上将PowerShell添加到上下文菜单中。也就是右键单击文件夹或文件夹内部以获取菜单,并启动PowerShell窗口而不会在启动后关闭它。这里的答案帮助我做到了这一点,我想在这里分享它,以防它能帮助其他人。
  • 按WIN + R启动注册表编辑器
  • 键入regedit.exe并按Enter,导航到HKEY_CLASSES_ROOT\Directory\Background\shell
  • 右键单击shell,创建一个密钥,给它一个名称,例如PowershellHere
  • 在右窗格中,双击默认值并提供描述性名称,例如Powershell Here
  • 右键单击先前创建的PowershellHere密钥,创建一个新密钥,并将其命名为"command",请确保名称完全相同但不包括引号。
  • 在右窗格中,双击默认值,然后键入下面的命令
  • C:\Windows\system32\WindowsPowerShell\v1.0\PowerShell.exe -noexit -Command CD '"%1"' -noexit标志确保Powershell窗口在启动后不会立即关闭 '"%1"'标志表示您右键单击的文件夹 -Command CD'"%1"'将确保Powershell进入右键单击的目录。

要使右键单击文件夹内的空白处起作用,请重复步骤,但是此时注册表位置为:

HKEY_CLASSES_ROOT\Directory\shell
命令为:
C:\Windows\system32\WindowsPowerShell\v1.0\PowerShell.exe -noexit

在 Windows 7 Ultimate SP1 上测试过,但我认为它对于较新版本的 Windows 也可能适用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接