How to run PowerShell in CMD
Categories:
Mastering PowerShell Execution within CMD
Unlock advanced scripting capabilities by seamlessly integrating PowerShell commands and scripts directly into your Command Prompt workflows. This guide covers various methods, best practices, and common pitfalls.
While Command Prompt (CMD) is a venerable tool for Windows users, PowerShell offers significantly more power and flexibility for system administration and automation. Fortunately, you don't always need to switch environments. This article will guide you through executing PowerShell commands and scripts directly from CMD, enhancing your command-line productivity.
Executing Single PowerShell Commands
The simplest way to run PowerShell from CMD is to execute a single command. You can do this using the powershell.exe
executable, followed by the -Command
(or -c
) parameter. This parameter tells PowerShell to run the specified string as a command and then exit.
powershell.exe -Command "Get-Date"
Executing Get-Date
PowerShell cmdlet from CMD.
powershell.exe -Command "Get-Service | Where-Object { $_.Status -eq 'Running' } | Select-Object Name, Status"
Running a more complex piped PowerShell command from CMD.
Running PowerShell Scripts from CMD
Executing an entire PowerShell script (.ps1
file) from CMD requires a slightly different approach. You'll still use powershell.exe
, but instead of -Command
, you typically specify the script's path. It's crucial to understand PowerShell's execution policy, which can prevent scripts from running.
powershell.exe -File "C:\Scripts\MyScript.ps1"
Running a PowerShell script located at a specific path.
Set-ExecutionPolicy RemoteSigned -Scope Process
to allow local scripts to run, but remember to revert it afterwards if security is a concern.Passing Arguments to PowerShell Scripts
You can pass arguments from CMD to your PowerShell scripts. These arguments are then accessible within the PowerShell script via the $args
automatic variable or by defining parameters in your script using the param
block.
param(
[string]$Name,
[int]$Age
)
Write-Host "Hello, $Name! You are $Age years old."
Write-Host "All arguments received: $($args -join ', ')"
A PowerShell script designed to accept parameters.
powershell.exe -File "C:\Scripts\MyScript.ps1" -Name "Alice" -Age 30
Running MyScript.ps1
and passing named arguments.
param
block makes argument passing clearer and less error-prone than relying solely on the $args
array.Understanding Execution Flow (CMD -> PowerShell -> CMD)
When you invoke powershell.exe
from CMD, a new PowerShell process is launched. This process executes the specified command or script and then typically terminates, returning control to the original CMD session. Understanding this flow is crucial for handling output and error codes.
Execution flow from CMD to PowerShell and back.
Capturing PowerShell Output in CMD Variables
You can capture the output of a PowerShell command in a CMD environment variable. This is particularly useful for integrating PowerShell's powerful data processing capabilities into traditional batch scripts.
FOR /F "delims=" %%i IN ('powershell.exe -Command "(Get-Date).ToString('yyyy-MM-dd')"') DO SET CurrentDate=%%i
ECHO The current date is: %CurrentDate%
Capturing PowerShell's Get-Date
output into a CMD variable.
FOR /F
command in CMD is incredibly versatile for processing the output of other commands, including those from PowerShell. Ensure your PowerShell command outputs a single string for easy capture.