In any Windows environment, you’ll inevitably encounter a mix of modern PowerShell scripts and legacy Command Prompt (cmd.exe) batch files. Instead of treating them as separate worlds, you can make them work together. Understanding how PowerShell and CMD can call each other is a crucial skill for any system administrator, allowing you to integrate legacy tools into modern automation workflows.

This guide will walk you through the techniques for calling CMD from PowerShell and PowerShell from CMD, enabling you to build powerful, hybrid scripts that leverage the best of both shells.


Part 1: Calling CMD from PowerShell

PowerShell is designed to be backward-compatible with traditional Windows commands, so executing cmd.exe commands or batch files is a seamless experience.

Simple Commands

You can type most common cmd.exe commands directly into a PowerShell prompt. PowerShell will execute them as native commands and return their text output.

# These cmd commands work directly in PowerShell
ipconfig /all
netstat -an

The output from these commands is a stream of plain text strings, which you can then pipe to PowerShell cmdlets like Select-String for further processing.

Complex Commands and Batch Files

For commands that use CMD-specific syntax (like for loops or set) or to run a batch file, it’s best to explicitly call cmd.exe.

  • cmd /c "...": Executes the command and then terminates the CMD session.
  • cmd /k "...": Executes the command and keeps the CMD session open.

Running a batch file:

# Assumes cleanup.bat is in the current directory
./cleanup.bat

Running a complex command:

cmd /c "set MYVAR=Hello && echo %MYVAR%"

Capturing CMD Output in PowerShell

You can easily capture the text output from a CMD command into a PowerShell variable.

$ipInfo = ipconfig | Select-String "IPv4 Address"
$ipAddress = ($ipInfo -split ':')[1].Trim()
Write-Host "Your IP address is: $ipAddress"

Part 2: Calling PowerShell from CMD

You can execute PowerShell commands or scripts from a Command Prompt or a batch file by calling the powershell.exe (for Windows PowerShell 5.1) or pwsh.exe (for PowerShell 7+) executable.

Running a Single Command with -Command

The -Command (or -c) parameter lets you execute a single line of PowerShell code.

@REM Get a list of all running services from a batch file
powershell.exe -Command "Get-Service | Where-Object { $_.Status -eq 'Running' }"

Quoting is important! It’s often best to wrap the PowerShell command in double quotes.

Executing a .ps1 Script with -File

This is the most common method for running a PowerShell script from CMD.

Example PowerShell script (C:\Scripts\Report.ps1):

param(
    [string]$UserName,
    [string]$Path
)
$message = "Report for user '$UserName' will be saved to '$Path'"
$message | Out-File -FilePath (Join-Path $Path "report.log")
Write-Host $message

Call it from a batch file:

@echo off
set USERNAME=Admin
set FOLDER=C:\Temp
pwsh.exe -File "C:\Scripts\Report.ps1" -UserName "%USERNAME%" -Path "%FOLDER%"

Capturing PowerShell Output in CMD

You can capture the output of a PowerShell command in a CMD for /f loop.

@REM Get the current date from PowerShell and store it in a CMD variable
for /f "delims=" %%a in ('powershell.exe -Command "Get-Date -Format 'yyyy-MM-dd'"') do set CURRENT_DATE=%%a
echo Today's date is: %CURRENT_DATE%

The Challenges: Quoting and Error Handling

When mixing shells, be aware of these common pitfalls:

  1. Quoting: CMD and PowerShell have different rules for quoting. A command that works in one shell might fail when called from the other due to a quoting issue. When in doubt, start with a simple command and gradually add complexity.

  2. Error Handling: The two shells have different ways of reporting errors.

    • In PowerShell, after running a native command, check the $LASTEXITCODE automatic variable. A value of 0 usually means success.
    • In CMD, after running a PowerShell command, check the %ERRORLEVEL% variable.
  3. Execution Policy: If your PowerShell scripts fail to run when called from CMD, it’s likely due to the PowerShell execution policy. You may need to set it to allow scripts to run.

    # Run this in PowerShell as an Administrator
    Set-ExecutionPolicy RemoteSigned -Scope CurrentUser


Conclusion

PowerShell and CMD are not mutually exclusive; they are two tools in the same toolbox. By understanding how to make them work together, you can:

  • Integrate legacy batch scripts into modern PowerShell automation workflows.
  • Transition to PowerShell gradually by wrapping older scripts.
  • Leverage the right tool for the right job, using CMD for its simplicity with certain tasks and PowerShell for its power with complex logic and data manipulation.

Mastering this interoperability is a key skill for any Windows administrator looking to build flexible, powerful, and maintainable automation.