Skip to main content

How to Check the Installed PowerShell Version in Batch Script

PowerShell has evolved from a simple Windows management framework to a powerful, cross-platform automation engine. Because many Batch scripts act as "Launchers" for more complex PowerShell (.ps1) files, it is crucial to verify that the version of PowerShell on the machine matches the script's requirements. For example, a script using modern objects and cmdlets might require PowerShell 5.1 or even PowerShell Core (v7). Using a Batch script to audit the PowerShell version ensures your environment is consistent and prevents "Command not found" or syntax errors.

This guide explains how to extract the version string effectively.

Why Check PowerShell Version via Batch?

  • Dependency Guarding: Ensuring the user has at least PowerShell 5.1 (the baseline for Windows 10/11) before launching an automation task.
  • Cross-Platform Compatibility: Identifying if the machine is using the system-built "Windows PowerShell" (v5.1) or the modern, standalone "PowerShell" (v7+).
  • Environment Bootstrapping: Automatically prompting the user to update their management framework if an old version (v2.0) is detected.
Desktop vs. Core
  • v5.1 and below: Known as "Windows PowerShell," built into the OS.
  • v6.0 and higher: Known as "PowerShell," often marketed as "PowerShell Core," installed as a separate, cross-platform app.

Method 1: Using the $PSVersionTable Object (Best Practice)

The most accurate way to find the version is to ask PowerShell itself to report its internal version table.

@echo off
echo [PROCESS] Querying PowerShell Version...
echo.

:: Call powershell and extract the 'PSVersion' property
powershell -NoProfile -Command "$PSVersionTable.PSVersion"

pause

Method 2: Extracting Version Info into a Variable

To use the version number in an IF statement (e.g., to stop if the version is less than 5), you should parse the output.

@echo off
setlocal

:: Check that PowerShell exists first
where powershell >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] PowerShell is not installed.
pause
exit /b 1
)

:: Capture the 'Major' version number
for /f "tokens=*" %%a in ('powershell -NoProfile -Command "$PSVersionTable.PSVersion.Major"') do set "PS_MAJOR=%%a"

if not defined PS_MAJOR (
echo [ERROR] Could not determine PowerShell version.
pause
exit /b 1
)

echo [INFO] Detected Major Version: %PS_MAJOR%

if %PS_MAJOR% LSS 5 (
echo [WARNING] Outdated PowerShell detected (v%PS_MAJOR%^).
echo [HELP] Please upgrade to at least version 5.1.
) else (
echo [SUCCESS] PowerShell %PS_MAJOR% is up to date.
)

pause

Method 3: Checking for PowerShell Core (v7+)

Modern PowerShell installs as pwsh.exe rather than powershell.exe. You can check if the user has the latest cross-platform version installed.

@echo off
echo [PROCESS] Checking for PowerShell Core (v7+^)...

where pwsh >nul 2>&1

if %errorlevel% equ 0 (
echo [SUCCESS] PowerShell Core is installed.
pwsh -Version
) else (
echo [INFO] PowerShell Core is NOT found in the system PATH.
echo [HELP] Download from https://github.com/PowerShell/PowerShell
)
pause

Creating a Resilient Framework Checker

This script checks for both PowerShell editions and provides a comprehensive environment report.

@echo off
setlocal

echo ============================================================
echo PowerShell Framework Auditor
echo ============================================================

:: 1. Check if Windows PowerShell is present
where powershell >nul 2>&1
if %errorlevel% neq 0 (
echo [CRITICAL] Windows PowerShell is MISSING from this system.
pause
exit /b 1
)

:: 2. Pull the Full Version string and Major version
set "FULL_VER="
for /f "tokens=*" %%v in ('powershell -NoProfile -Command "$PSVersionTable.PSVersion.ToString()"') do set "FULL_VER=%%v"

set "PS_MAJOR="
for /f "tokens=*" %%m in ('powershell -NoProfile -Command "$PSVersionTable.PSVersion.Major"') do set "PS_MAJOR=%%m"

echo.
echo Windows PowerShell: %FULL_VER%

:: 3. Check for PowerShell Core (v7+)
set "PWSH_VER="
where pwsh >nul 2>&1
if %errorlevel% equ 0 (
for /f "tokens=*" %%v in ('pwsh -NoProfile -Command "$PSVersionTable.PSVersion.ToString()"') do set "PWSH_VER=%%v"
echo PowerShell Core: %PWSH_VER%
) else (
echo PowerShell Core: Not installed
)

:: 4. Version assessment
echo.
if defined PS_MAJOR (
if %PS_MAJOR% GEQ 5 (
echo [STATUS] Windows PowerShell %FULL_VER% meets baseline requirements.
) else (
echo [WARNING] Windows PowerShell %FULL_VER% is outdated.
)
)

echo ============================================================
pause

Common Pitfalls and How to Avoid Them

Slower Initialization

Calling powershell -Command causes a new process to spin up, which can take 1-2 seconds.

Wrong Way:

:: Calling PowerShell inside a Tight Loop
for /L %%i in (1,1,100) do powershell -Command "something"
:: This will be extremely slow.

Correct Way: Gather all the PowerShell data at the beginning of your script once and store it in Batch variables to use throughout the rest of your logic.

Missing -NoProfile

If you call powershell without -NoProfile, it will try to load the user's profile scripts, which can slow down the detection or cause unrelated errors.

SEO and UX Tip

Always use the -NoProfile flag when calling PowerShell from a Batch script. This ensures a "clean" execution that is faster and won't be affected by custom user scripts or modules.

Best Practices for Management Frameworks

  1. Check for Execution Policy: Knowing the version is one thing; knowing if you can run scripts is another. Check powershell -NoProfile -Command "Get-ExecutionPolicy".
  2. Verify Modules: If your script requires the "ActiveDirectory" or "Azure" modules, check for them using powershell -NoProfile -Command "Get-Module -ListAvailable".
  3. Encourage v7: If your script is used in a high-security environment, encourage users to install PowerShell 7, as it receives more frequent security updates than the built-in v5.1.
v2.0 Side-by-Side

Note that modern Windows installs often have a legacy "PowerShell 2.0" engine that can be enabled. However, querying $PSVersionTable will always show you the version of the engine currently executing the command.

Conclusion

Checking the PowerShell version via Batch script is a critical step for modern IT administrators and developers who rely on hybrid automation. By accurately identifying whether a system is running legacy v2.0, standard v5.1, or modern v7+, you can ensure that your automation routines are stable, compatible, and feature-rich. This proactive approach to platform verification reduces support tickets and ensures that your sophisticated management tools run on a verified and healthy framework, maintaining the integrity of your entire infrastructure.