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

问题描述 投票:0回答:7

我有一个非常短的 PowerShell 脚本,用于连接到服务器并导入 AD 模块。我想通过双击来运行脚本,但恐怕窗口在最后一行后立即关闭。

我该如何解决这个问题?

active-directory powershell-2.0
7个回答
162
投票

您基本上有 3 个选项来阻止 PowerShell 控制台窗口关闭,我在我的博客文章中更详细地描述了这些

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

请参阅我的博客以获取更多信息和下载脚本,该脚本将为您更改注册表。


31
投票

呃…… 我应该知道:

powershell -noexit <path\script> 

这就是全部:)


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
投票

只需在脚本底部的新行上添加暂停,就像在批处理文件中一样。


2
投票

如果您只想让窗口保持打开状态,则可以在脚本末尾添加

pause
,或者如果您希望之后能够运行命令,则可以添加
powershell
(显然,如果其他人都会使用您的代码)。


1
投票

就我自己而言,我想将 powershell 添加到 Windows 7 上的上下文菜单中。即右键单击文件夹或文件夹内部以获取启动 Powershell 窗口的菜单,而无需在启动后关闭它。这里的答案帮助我做到了这一点,我想在这里分享它,以防它对其他人有帮助。

  • 按 WIN + R 启动注册表编辑器 输入 regedit.exe 并按 Enter
  • 导航至 HKEY_CLASSES_ROOT\Directory\Background\shell
  • 右键单击 shell 并创建一个密钥并为其命名,例如 PowershellHere
  • 在右侧窗格中,双击“默认”并提供描述性名称,例如“Powershell Here”
  • 右键单击您之前创建的 PowershellHere 键,然后创建一个新键并将其命名为“command”,请确保您的名称准确无误,但不带引号。
  • 在右侧窗格中,双击“默认”,然后键入以下命令
  • C:\Windows\system32\WindowsPowerShell 1.0\PowerShell.exe -noexit -Command CD '"%1"' -noexit 标志确保 Powershell 窗口在启动后不会立即再次关闭 '“%1”'标志代表您右键单击的文件夹 -命令 CD '“%1”' 将确保 Powershell 更改为右键单击的目录。

要使右键单击在文件夹内起作用(即右键单击文件夹内的空白区域),请重复这些步骤,但这一次,注册表位置是:

HKEY_CLASSES_ROOT\目录\shell
命令是:
C:\Windows\system32\WindowsPowerShell 1.0\PowerShell.exe -noexit

在 Windows 7 Ultimate sp1 上进行了测试,但我想我也可能适用于更高版本的 Windows


0
投票

所有答案对我来说似乎都已经过时了,指的是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 命令现在似乎有一个全局位置

© www.soinside.com 2019 - 2024. All rights reserved.