How to run PowerShell in CMD

Learn how to run powershell in cmd with practical examples, diagrams, and best practices. Covers windows, powershell, batch-file development techniques with visual explanations.

Mastering PowerShell Execution within CMD

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.

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.

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.

A flowchart diagram illustrating the execution flow from CMD to PowerShell and back. Start node: 'CMD Session'. Arrow to 'powershell.exe Invoked'. Arrow to 'PowerShell Process Executes Command/Script'. Arrow to 'PowerShell Process Exits'. Arrow back to 'CMD Session Resumes'. Labels clearly show the transition of control.

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.