CMD返回ErrorLevel而不运行程序

CMD返回ErrorLevel而不运行程序,cmd,errorlevel,nul,Cmd,Errorlevel,Nul,我正在通过CMD提示符运行Powershell命令,我想在开始运行命令之前先检查是否安装了Powershell。如果powershell不存在,我希望脚本退出,但不显示下面的实际错误。这是我的剧本: @echo off setlocal enabledelayedexpansion :: Check to see if Powershell is installed powershell.exe -command {"test"} > NUL if errorlevel 1 ( e

我正在通过CMD提示符运行Powershell命令,我想在开始运行命令之前先检查是否安装了Powershell。如果powershell不存在,我希望脚本退出,但不显示下面的实际错误。这是我的剧本:

@echo off
setlocal enabledelayedexpansion
:: Check to see if Powershell is installed
powershell.exe -command {"test"} > NUL
if errorlevel 1 (
    echo/Powershell is NOT Installed
    EXIT
) else (
    goto PSI
)

:PSI
powershell Set-ExecutionPolicy RemoteSigned
我遇到的问题是,我将此作为输出:

Powershell is NOT Installed

'powershell.exe' is not recognized as an internal or external command,
operable program or batch file.

明白了!我不得不使用2>NUL而不是NUL来重定向输出:

:: Check to See if Powershell is Installed
powershell.exe test 2>NUL
    if errorlevel 1 (
        echo/Powershell is NOT Installed
    EXIT
    ) else (
    goto PSI
    )

实际上,上面的powershell命令也会出错。我必须使用powershell“写入输出''n''2>NUL