Run PowerShell commands and scripts from CMD
Sample output — your paths and versions will differ.
Documentation: Microsoft Learn
Inspect a sample
Example reader
Inspect this page's example
Run supported commands against the practice data, or inspect the example without executing it.
Ready to inspect.
Show reference output
Inspect this example’s structure
Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.
Get-Command pwsh, powershell -ErrorAction SilentlyContinue | Select-Object Name, Source- Stage 1Get-Command
-ErrorAction - Stage 2Select-Object
Positional values or no named parameters
Choose the engine
powershell.exe launches Windows PowerShell; pwsh launches PowerShell 7 when installed and discoverable. Use a full executable path for unattended jobs when PATH could select a different installation. NoProfile skips your profile scripts so the command starts clean; it does not change what the command is allowed to do. If a script refuses to start with an execution-policy error, that is PowerShell declining to run script files under the current policy — the Execution Policy lesson explains what the policy means and how to read it. Do not silence it with -ExecutionPolicy Bypass: that switch skips every policy check for that process.
Use File for a saved script
File takes a script path followed by its arguments. Put options for the PowerShell executable before -File. Quote paths containing spaces. Keep complex loops and nested quoting in a .ps1 file so CMD does not have to parse the same expression as PowerShell.
Use Command for a short expression
Command is useful for a short pipeline. Enclose the complete PowerShell expression in double quotes at the CMD layer; single quotes inside it can delimit a PowerShell string. CMD expands percent variables and interprets some metacharacters before PowerShell starts.
Return a meaningful exit code
Check %ERRORLEVEL% in CMD immediately after the process exits. A script should use an explicit exit code when an automation contract depends on success or failure. Printing a warning alone is not a reliable process-level failure signal.
Troubleshoot the same launch path
Compare executable, account, working directory and environment variables with the successful interactive run. Use an absolute script path and capture output/error streams in the job runner. Do not disable execution policy as a universal fix for an unrelated quoting or permission problem.
Command-line reference
Show reference output
The code above is a reference for your own authorized environment. The output shown is a stored fictional example — run the command in your own terminal to inspect your installation.
Read the original documentation
Windows PowerShell command-line options · PowerShell 7 command-line options
Check your understanding
There is no runnable lab on this page — these run in your own terminal, so predict the answer first, then verify.
Q1. From CMD you run pwsh -NoProfile -Command "$PSVersionTable.PSVersion". Do you get a version number or an error?
Answer: A version number like 7.4.x — -Command runs the expression and prints the result, then exits.
Q2. You run powershell.exe -File "C:\Ops\Inventory.ps1" and get an execution-policy error. What is the safe next step?
Answer: Read the current policy with Get-ExecutionPolicy and learn what it means in the Execution Policy lesson — do not reach for -ExecutionPolicy Bypass, which skips every policy check for that process.
Q3. After your script runs, echo %ERRORLEVEL% prints 0. What does that tell you?
Answer: The PowerShell process exited reporting success — but only because the script set an explicit exit code or finished without a terminating error. A printed warning alone never sets a nonzero exit code.