尝试在批处理文件中使用START 实用程序,它将在新窗口中启动程序并允许批处理的cmd 窗口在后台退出。
START powershell -file "D:\scripts\Powershell\xxx.ps1"
注意START 在first parameter contains double quotes 时会表现出[可能] 意外行为。
如果您正在使用 GUI 元素或外部应用程序,并且想要隐藏 Powershell 的窗口而不是 GUI,请尝试以下操作:
START powershell -WindowStyle Hidden "D:\scripts\Powershell\xxx.ps1"
这是我如何将批处理与 PowerShell GUI 结合使用的示例:
C:\call.cmd 的内容:
START "" powershell -NoLogo -WindowStyle Hidden "C:\gui.ps1"
C:\gui.ps1 的内容:
# VB Assembly GUI example
Add-Type -AssemblyName Microsoft.VisualBasic
[Microsoft.VisualBasic.Interaction]::MsgBox(
"Do you like VB in PowerShell?",
[Microsoft.VisualBasic.MsgBoxStyle]::YesNo,
"Hello"
)
# Windows Forms GUI example
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.MessageBox]::Show(
"Do you like Windows Forms?",
"Hello",
[System.Windows.Forms.MessageBoxButtons]::YesNo
)
# External app GUI example
& notepad.exe
运行call.cmd 批处理将使用START 启动PowerShell [退出cmd],PowerShell 将隐藏其窗口,但显示从PS1 文件执行的GUI 元素。