【问题标题】:using a batch file to run a ps1 file but hide the command prompt after start?使用批处理文件运行 ps1 文件但在启动后隐藏命令提示符?
【发布时间】:2013-12-01 18:42:25
【问题描述】:

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

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

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

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

谢谢

【问题讨论】:

    标签: powershell batch-file powershell-2.0 powershell-3.0


    【解决方案1】:

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

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

    注意STARTfirst 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 元素。

    【讨论】:

    • 嘿,很高兴再次见到你。现在我打开了一个空的 Powershell 窗口 + 我的程序。有没有办法让我的程序?
    • xxx.ps1 脚本是启动外部应用程序,还是创建自己的 GUI?只是想了解您到底需要什么。
    • 我添加了一个改编自我以前使用过的一些东西的示例。它应该允许您从 PS1 显示 GUI 元素,但隐藏 PowerShell 窗口。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-02
    • 2021-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多