批处理文件运行Powershell并隐藏输出



我有一个批处理文件来执行PowerShell脚本:

C:WindowsSystem32WindowsPowerShellv1.0powershell.exe  -NoLogo -NonInteractive -NoProfile -ExecutionPolicy Bypass -command .MyPowerShell.ps1
echo message1
echo message2

批处理可以毫无问题地运行PowerShell,但CMD提示符会显示PowerShell脚本的输出。是否可以在CMD提示符中隐藏PowerShell脚本的输出?我仍然需要在CMD提示符中传递一些消息,所以终止CMD不是一个选项。任何帮助都将不胜感激!

Powershell有自己的重定向,但鉴于您正在从cmd运行powershell,并且不想看到输出,请将输出重定向到nul

@echo off
"%windir%System32WindowsPowerShellv1.0powershell.exe"  -NoLogo -NonInteractive -NoProfile -ExecutionPolicy Bypass -command .MyPowerShell.ps1 >nul 2>&1
echo message1
echo message2
@echo off
C:WindowsSystem32WindowsPowerShellv1.0powershell.exe  -NoLogo -NonInteractive -NoProfile -ExecutionPolicy Bypass -command .MyPowerShell.ps1
cls
echo message1
echo message2
pause

我使用命令cls清除批处理脚本以将其隐藏

最新更新