我用powershell构建了一个小工具,并通过批处理文件打开它。
批处理文件具有以下内容:

powershell -file "D:\scripts\Powershell\xxx.ps1"

现在它打开我的工具,但总是在后台显示命令提示符。我希望在运行我的文件后隐藏 CMD。

如何做到这一点?我试过 -hidden 但后来我的程序被隐藏了:D

谢谢

最佳答案

尝试在批处理文件中使用 START 实用程序,它将在新窗口中启动程序并允许批处理的 cmd 窗口在后台退出。

START powershell -file "D:\scripts\Powershell\xxx.ps1"

请注意,当 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

运行 START 批处理将使用 call.cmd 启动 PowerShell [退出 START ],PowerShell 将隐藏其窗口,但会显示从 PS1 文件执行的 GUI 元素。

关于powershell - 使用批处理文件运行 ps1 文件但在启动后隐藏命令提示符?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20050772/

10-13 07:46
查看更多