Windows CMD (Command Prompt)¶
The legacy Windows shell. CMD is everywhere and fine for quick tasks, but for scripting, sensible pipelines, and Linux parity, prefer PowerShell — this sheet focuses on what still gets done in CMD and how it maps to Linux.
Table of Contents¶
- Installation / Setup
- Core Syntax
- Common Operations
- Practical Examples
- Common Patterns
- Best Practices
- Common Mistakes
- Troubleshooting
- Related Cheat Sheets
- Official Documentation
Installation / Setup¶
- Open:
Win+R→cmd→ Enter; or Terminal → new tab → Command Prompt. - Run as administrator when a task needs elevation (right-click → Run as administrator).
ver :: Windows version
echo %PATH% :: print an environment variable (%VAR% in CMD)
help :: brief help per command (help cd, help dir)
Distinguish CMD from PowerShell up front: CMD is a token-based batch shell using %VAR%, copy, dir; PowerShell is an object shell using $VAR, Copy-Item, Get-ChildItem. If you type something and PowerShell errors, you may be mixing them — this sheet is CMD.
Core Syntax¶
Linux ↔ Windows (CMD) quick map¶
| Task | Linux | CMD |
|---|---|---|
| list files | ls -la |
dir / dir /a |
| change dir | cd /data |
cd \data (and cd /d D:\ to change drive) |
| print working dir | pwd |
cd (no args) |
| copy | cp a b |
copy a b |
| move/rename | mv a b |
move a b / ren a b |
| delete file(s) | rm f |
del f |
| delete tree | rm -r d |
rmdir /s /q d |
| make dir | mkdir -p a/b |
mkdir a\b |
| view file | cat f / less f |
type f / more f |
| clear screen | clear |
cls |
| find text | grep pattern f |
findstr pattern f |
| show PATH | echo $PATH |
echo %PATH% |
| history | history |
doskey /history |
| whoami | whoami |
whoami |
Common Operations¶
Navigation & directories¶
dir :: list
dir /a :: include hidden
dir /s *.py :: recursive search for files
cd \Users\me\project :: absolute path
cd .. :: up one level
cd /d E:\data :: change drive too
mkdir data\raw :: nested creation works
rmdir /s /q olddir :: delete tree, no prompt (dangerous)
Files¶
copy file.txt backup\ :: copy
move file.txt ..\done\ :: move
ren report.csv report-old.csv :: rename
del *.tmp :: delete matching
type README.md :: print file
more bigfile.log :: page through
fc file1.txt file2.txt :: compare files
Search & text¶
findstr /i "error" app.log :: case-insensitive search
findstr /n "TODO" *.py :: with line numbers
findstr /c:"exact phrase" log.txt
dir /s /b *.csv | findstr /i train :: pipe into findstr
Processes & system¶
tasklist :: list processes
tasklist | findstr python :: find one
taskkill /PID 1234 /F :: force kill by PID
taskkill /IM python.exe /F :: kill by image name
systeminfo :: OS, RAM, uptime
wmic cpu get name :: CPU model
ver :: Windows version
Networking¶
ping -n 4 google.com
ipconfig :: IP config (all adapters)
ipconfig /all
netstat -ano :: ports & owning PIDs
netstat -ano | findstr :8888
nslookup example.com
tracert example.com
curl -I https://example.com :: Win10+ ships curl
Environment variables & archives¶
echo %USERNAME% :: current user
echo %PATH%
set MY_VAR=hello :: session only
setx MY_VAR hello :: persistent (new shells)
set PATH=%PATH%;C:\tools :: extend for this session
tar -czf backup.tar.gz folder\ :: Win10+ tar built in
tar -xzf backup.tar.gz
Expand-Archive file.zip dest\ :: PowerShell — see powershell.md
:: classic alternative: use Explorer right-click > Extract
SSH (Windows 10/11 OpenSSH client)¶
ssh user@host
ssh -i C:\Users\me\.ssh\id_ed25519 user@host
scp -i C:\Users\me\.ssh\id_ed25519 model.pt user@host:/home/user/
:: keys live in C:\Users\you\.ssh\
Services & troubleshooting¶
net start :: list running services
net start <name> :: start a service
net stop <name> :: stop
sc query <name> :: query a service
sc queryex mysql :: detail incl. PID
sfc /scannow :: repair system files
chkdsk C: :: check disk (may need admin/reboot)
DISM /Online /Cleanup-Image /RestoreHealth :: repair image (admin)
Practical Examples¶
1. Find what is using a port¶
2. Batch-rename files¶
:: rename all .TXT to .txt (case fix) in current folder
for %f in (*.TXT) do ren "%f" "%~nf.txt"
:: in a .bat file, double the %: for %%f in (*.TXT) do ren "%%f" "%%~nf.txt"
3. Daily backup with timestamp¶
set stamp=%date:~0,4%-%date:~5,2%-%date:~8,2%
mkdir backups\%stamp%
copy models\*.pt backups\%stamp%\
4. Log viewer tail¶
powershell -Command "Get-Content app.log -Tail 50 -Wait"
:: CMD has no tail; delegate to PowerShell when needed
Common Patterns¶
:: Conditional execution (same idea as Linux && and ||)
dir models && echo found || echo missing
:: Run as admin from a script: check elevation
net session >nul 2>&1 && (echo admin) || (echo not admin & pause)
:: Loop over files (batch file — %% in .bat, % at prompt)
for %%f in (data\*.csv) do python prep.py "%%f"
:: Quietly discard output (2>nul = stderr)
ping -n 1 host >nul 2>&1 && echo host up || echo host down
Best Practices¶
- Prefer PowerShell for anything non-trivial — CMD has no objects, weak quoting, and painful loops.
- Use
rmdir /s /qanddelwith care — CMD has no trash and no undo. setxaffects only new shells; restart the terminal after setting vars.- Quote paths with spaces throughout:
cd "C:\Program Files\x". - Escape special chars with
^(e.g.echo a ^& b), and use%not$for variables. - In .bat files use
%%in loops; at the interactive prompt use%. - Keep to
curl(built in) rather than the ancientftptool. - Defensive/system commands (
netstat,tasklist,sc query,sfc) are read-only and safe; avoid running destructive variants on systems you don't own.
Common Mistakes¶
| Mistake | Consequence | Fix |
|---|---|---|
Using ls in CMD |
'ls' is not recognized |
dir |
Using $var instead of %var% |
Literal text $var |
%VAR% in CMD, $VAR in PowerShell |
cd D:\data doesn't switch drive |
Stays on C: | cd /d D:\data |
rmdir /s without checking |
Deletes a whole tree | List first: dir /s |
%i in a .bat loop |
Script error | Double it: %%i |
| Forgetting quotes around paths with spaces | "Too many arguments" | Always quote |
netstat output truncated |
PID hidden | Use -ano and widen the window |
Troubleshooting¶
| Symptom | Cause | Fix |
|---|---|---|
'cmd' is not recognized... |
Not actually in CMD (PowerShell) | Check prompt > vs PS> |
Access is denied |
Needs admin | Run as administrator |
| Port already in use | Another app | netstat -ano \| findstr :port → kill PID |
curl unknown (old Windows) |
Legacy build | Use PowerShell Invoke-WebRequest or install curl |
| Batch file closes instantly | Error with no pause | Run from an open CMD window; add pause |
%%f error at prompt |
Double-percent at prompt | Use single %f interactively |
Related Cheat Sheets¶
- PowerShell — the modern Windows shell (preferred)
- WSL — real Linux bash on Windows
- linux-commands.md — Linux equivalents
- bash.md — scripting if you can use WSL/Git Bash
- Home · Central Navigation Index
Official Documentation¶
Last Updated¶
2026-09-06 — Initial version.