0

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.
aschipfl
  • 33,626
  • 12
  • 54
  • 99
Ken J
  • 4,312
  • 12
  • 50
  • 86

1 Answers1

1

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
    )
Ken J
  • 4,312
  • 12
  • 50
  • 86
  • Actually, the above powershell command errors out as well. I had to use powershell "Write-Output ''n'" 2>NUL – Ken J Jun 15 '12 at 22:15