/cheatsheets/cmd/cmd-cheat-sheet
cmd cheat sheet: files, variables, batch syntax and network commands
verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash)updated
- tool
- Command Prompt (cmd)
- versions
- Windows 10, Windows 11, Windows Server 2016+
- os
- windows
Command Prompt (cmd.exe) commands grouped by task. Every built-in command prints its help with /? (for example for /?). In batch files, loop variables are written %%a; at the prompt, %a.
Files and folders
| Task | Command |
|---|---|
| Current folder / change folder and drive | cd / cd /d D:\work |
| List names only / folders only / recursive | dir /b / dir /b /ad / dir /b /s *.log |
| Make a folder (with parents) | mkdir a\b\c |
| Copy / move / rename | copy a.txt b.txt / move a.txt dir\ / ren a.txt b.txt |
| Copy a tree | robocopy src dst /e (or xcopy src dst /e /i) |
| Delete a file / a folder tree | del file.txt / rd /s /q folder |
| Print a file / create a file | type file.txt / > file.txt echo text |
| Search text | findstr /i /n "error" *.log |
| Where is a program | where git |
Variables and strings
set "NAME=errcodex-report.txt"
echo whole: %NAME%
echo first 8 chars: %NAME:~0,8%
echo last 3 chars: %NAME:~-3%
echo replace: %NAME:report=summary%
set /a SUM=7*6
echo arithmetic: %SUM%
if defined NAME echo NAME is defined
if not defined NOPE echo NOPE is not defined
if /i "%NAME:~-3%"=="TXT" echo case-insensitive compare matchedOutput
whole: errcodex-report.txt first 8 chars: errcodex last 3 chars: txt replace: errcodex-summary.txt arithmetic: 42 NAME is defined NOPE is not defined case-insensitive compare matched
| Syntax | Meaning |
|---|---|
| %VAR:~start,length% | substring; negative start counts from the end |
| %VAR:old=new% | replace every occurrence of old (case-insensitive) |
| set /a X=Y*2+1 | integer arithmetic |
| %~1, %~dp0, %~n1, %~x1 | argument without quotes; folder of the script; name; extension |
| %CD%, %DATE%, %TIME%, %RANDOM%, %ERRORLEVEL% | dynamic variables |
Batch syntax
for %%f in (a.txt b.txt) do > %%f echo data
for %%f in (*.txt) do echo found %%f
for /l %%i in (1,1,3) do echo counter %%i
call :greet World
goto :eof
:greet
echo Hello, %~1
exit /b 0Output
found a.txt found b.txt counter 1 counter 2 counter 3 Hello, World
| Construct | Example |
|---|---|
| if / else | if exist "out\" (echo yes) else (echo no) |
| Compare numbers | if %N% GEQ 10 echo big (EQU NEQ LSS LEQ GTR GEQ) |
| Loop over files / numbers / lines | for %%f in (*.log) / for /l %%i in (1,1,10) / for /f |
| Subroutine | call :label args ... exit /b |
| Run on success / failure | cmd1 && cmd2 / cmd1 || cmd2 |
| Redirect output / errors / both | > out.txt / 2> err.txt / > all.txt 2>&1 |
| Discard output | > nul 2>&1 |
System and network
These print values of the machine they run on:
ipconfig /all
ipconfig /flushdns
netstat -ano | findstr :443
tasklist /fi "imagename eq node.exe"
systeminfo | findstr /b /c:"OS Name" /c:"OS Version"
hostname
whoami| Command | Shows |
|---|---|
| ipconfig /all | every adapter: IP, mask, gateway, DNS servers, MAC (Physical Address), DHCP |
| ipconfig /flushdns | clears the DNS resolver cache |
| netstat -ano | connections and listening ports with the owning PID (-a all, -n numeric, -o PID) |
| netstat -ano | findstr :8080 | which PID uses port 8080; then tasklist /fi "pid eq PID" |
| tasklist / tasklist /svc | running processes / services hosted in each process |
| systeminfo | OS version and build, install date, boot time, memory, hotfixes |
Sources
- learn.microsoft.comlearn.microsoft.com/en-us/windows-server/administration/windows-commands/windows-commands
- learn.microsoft.comlearn.microsoft.com/en-us/windows-server/administration/windows-commands/set_1
- learn.microsoft.comlearn.microsoft.com/en-us/windows-server/administration/windows-commands/netstat
- learn.microsoft.comlearn.microsoft.com/en-us/windows-server/administration/windows-commands/tasklist
Related pages
commands · cmdCheck %ERRORLEVEL% in a batch file (if errorlevel, ||, exit /b)Read a command's exit code in cmd with %ERRORLEVEL%, test it with if errorlevel N (true for N or higher) or && and ||, and return a code with exit /b N.commands · cmdfor /f in cmd: read a file line by line or capture a command's outputfor /f loops over the lines of a file, a string or a command's output and splits them with tokens and delims. Use %%a in batch files and usebackq for spaces.compare · cmdxcopy vs robocopy: which one to use for copying foldersrobocopy retries, mirrors, resumes and logs, so use it for backups and large folder trees; xcopy is older and simpler, with no mirror, retry or multithreading.cheat sheets · powershellPowerShell cheat sheet: files, objects, filtering, strings and error handlingPowerShell cheat sheet: find commands and help, work with files, filter and sort objects, format strings, hashtables, loops, errors and bash equivalents.