使用批处理文件运行ps1文件但启动后隐藏命令提示符?

Ray*_*and 3 powershell batch-file powershell-2.0 powershell-3.0

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

powershell -file "D:\scripts\Powershell\xxx.ps1"
Run Code Online (Sandbox Code Playgroud)

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

怎么实现呢?我试过-hidden 但是我的整个程序都被隐藏了:D

谢谢

jsc*_*ott 6

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

START powershell -file "D:\scripts\Powershell\xxx.ps1"
Run Code Online (Sandbox Code Playgroud)

START当第一个参数包含双引号时,请注意展示[可能]意外行为.

如果您正在使用GUI元素或外部应用程序,并且想要隐藏Powershell的窗口而不是GUI,请尝试以下操作:

START powershell -WindowStyle Hidden "D:\scripts\Powershell\xxx.ps1"
Run Code Online (Sandbox Code Playgroud)

以下是我与PowerShell GUI一起使用批处理的示例:

C:\ call.cmd的内容:

START "" powershell -NoLogo -WindowStyle Hidden "C:\gui.ps1"
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

运行call.cmd批处理将用于START启动PowerShell [exiting cmd],PowerShell将隐藏其窗口,但以其他方式显示从PS1文件执行的GUI元素.