Question

I'm running Powershell commands through a CMD prompt and I'd like to check if Powershell is installed first before I start running my commands. I'd like the script to exit if powershell doesn't exist without showing the actual error below. Here's my script:

@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

The problem that I'm having is that I'm getting this as output:

Powershell is NOT Installed

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

Solution

Figured it out! I had to use 2>NUL instead of NUL to redirect output:

:: Check to See if Powershell is Installed
powershell.exe test 2>NUL
    if errorlevel 1 (
        echo/Powershell is NOT Installed
    EXIT
    ) else (
    goto PSI
    )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top