gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 10:51:08 24 4
gpt4 key购买 nike

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

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

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

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

谢谢

最佳答案

尝试使用 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 - 使用批处理文件运行 ps1 文件但在启动后隐藏命令提示符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20050772/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com