How to Run CMD Commands in PowerShell

To run a CMD command in PowerShell, type it as is when it’s a real program such as ping or ipconfig. For commands built into Command Prompt, like dir /b or mklink, hand them to cmd.exe with /c:

ipconfig
cmd /c dir /b
cmd /c mklink /J .\latest .\reports

In this tutorial, we’ll build a small practice folder and run each kind of CMD command against it, then check where PowerShell 7 and Windows PowerShell 5.1 behave differently.

Build the practice folder

Open PowerShell 7 or Windows PowerShell 5.1 (no admin needed) and move to any scratch folder. If you haven’t compared the two shells yet, read how Windows PowerShell differs from CMD first.

This script creates a cmd-lab folder with three sales reports and a tiny batch file in a folder whose name has a space:

New-Item -ItemType Directory -Path .\cmd-lab\reports, '.\cmd-lab\Sales Tools' -Force | Out-Null
'Maria Lopez,Austin,1250' | Set-Content .\cmd-lab\reports\austin.csv
'Tom Baker,Denver,320' | Set-Content .\cmd-lab\reports\denver.csv
'Dana Reyes,Seattle,780' | Set-Content .\cmd-lab\reports\seattle.csv
'@echo off', 'echo Daily report for %1 is ready.' |
    Set-Content '.\cmd-lab\Sales Tools\daily-report.cmd'
Get-ChildItem .\cmd-lab -Recurse -File -Name

Output:

reports\austin.csv
reports\denver.csv
reports\seattle.csv
Sales Tools\daily-report.cmd

The daily-report.cmd file just echoes its first argument (%1). We’ll use it to test paths with spaces and exit codes later.

Step 1: Run CMD programs that work as is

Many Command Prompt commands are separate .exe files in C:\Windows\System32. PowerShell runs them directly, with the same switches you’d type in CMD:

ping -n 1 localhost | Select-String 'Packets'
ipconfig | Select-String 'IP Configuration'
where.exe ping

Output:

    Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
Windows IP Configuration
C:\Windows\System32\PING.EXE

I filtered the ping and ipconfig output with Select-String so no addresses show. The last line proves where.exe found the real ping program, which is also how you find the location of an executable.

Step 2: Watch for aliases that hijack CMD names

Some CMD names are PowerShell aliases. Let’s ask PowerShell what each name really runs:

foreach ($name in 'where', 'sc', 'dir', 'copy') {
    $c = Get-Command $name | Select-Object -First 1
    '{0,-5} -> {1} {2}' -f $name, $c.CommandType, $c.Definition
}

Output:

where -> Alias Where-Object
sc    -> Application C:\WINDOWS\system32\sc.exe
dir   -> Alias Get-ChildItem
copy  -> Alias Copy-Item

The output above is from PowerShell 7. In Windows PowerShell 5.1, sc was Alias Set-Content instead of sc.exe. That one difference can quietly create files:

sc query Spooler | Select-String 'STATE'
"File named query created: $(Test-Path .\query)"
sc.exe query Spooler | Select-String 'STATE'

Output:

        STATE              : 4  RUNNING
File named query created: False
        STATE              : 4  RUNNING
Windows PowerShell 5.1 console where sc query Spooler creates a file named query instead of querying the service
In 5.1, sc is Set-Content, so only sc.exe queries the service (Windows PowerShell 5.1)

In 5.1, the first line printed nothing and w