如何静默运行 PowerShell 命令?

hat*_*san 8 powershell

我想在没有蓝屏的情况下静默执行PowerShell命令。

如何从PowerShell命令执行此操作?

我试过这个。. .

PowerShell.exe -windowstyle hidden 
Run Code Online (Sandbox Code Playgroud)

但它不起作用 - 命令已执行但蓝屏仍然存在。

Vom*_*yle 5

从提示中静默运行 PowerShell 命令

就像声明的那样。. .

"您可以使用 PowerShell.exe 从其他工具的命令行启动 PowerShell 会话,例如 Cmd.exe,或在 PowerShell 命令行中使用它启动新会话。使用参数自定义会话。 "


-WindowStyle

设置会话的窗口样式。有效值为正常、最小化、最大化和隐藏。

-命令

执行指定的命令(和任何参数),就像在 PowerShell 命令提示符下键入它们一样,然后退出,除非指定了 NoExit 参数。本质上,之后的任何文本 -Command都作为单个命令行发送到 PowerShell


句法

powershell -windowstyle hidden -command <PowerShell Command String>
Run Code Online (Sandbox Code Playgroud)

可验证的例子

1.命令提示符(cmd)

powershell -windowstyle hidden -command get-childitem -path c:\ ^| out-file "C:\Folder\Log\log.txt"
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

注意: 使用 cmd 时,[ |] 管道符号需要使用 [ ^] 插入符号进行转义,因此“ ^|”。


2. PowerShell 提示

powershell -windowstyle hidden -command get-childitem -path c:\ | out-file "C:\Folder\Log\log.txt"
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

注意: 运行后,打开log.txt以验证其内容,因为out-file它会直接输出。


更多资源