Skip to content

/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

TaskCommand
Current folder / change folder and drivecd / cd /d D:\work
List names only / folders only / recursivedir /b / dir /b /ad / dir /b /s *.log
Make a folder (with parents)mkdir a\b\c
Copy / move / renamecopy a.txt b.txt / move a.txt dir\ / ren a.txt b.txt
Copy a treerobocopy src dst /e (or xcopy src dst /e /i)
Delete a file / a folder treedel file.txt / rd /s /q folder
Print a file / create a filetype file.txt / > file.txt echo text
Search textfindstr /i /n "error" *.log
Where is a programwhere git

Variables and strings

cmd
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 matched

Output

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
SyntaxMeaning
%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+1integer arithmetic
%~1, %~dp0, %~n1, %~x1argument without quotes; folder of the script; name; extension
%CD%, %DATE%, %TIME%, %RANDOM%, %ERRORLEVEL%dynamic variables

Batch syntax

cmd
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 0

Output

found a.txt
found b.txt
counter 1
counter 2
counter 3
Hello, World
ConstructExample
if / elseif exist "out\" (echo yes) else (echo no)
Compare numbersif %N% GEQ 10 echo big (EQU NEQ LSS LEQ GTR GEQ)
Loop over files / numbers / linesfor %%f in (*.log) / for /l %%i in (1,1,10) / for /f
Subroutinecall :label args ... exit /b
Run on success / failurecmd1 && 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:

cmd
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
CommandShows
ipconfig /allevery adapter: IP, mask, gateway, DNS servers, MAC (Physical Address), DHCP
ipconfig /flushdnsclears the DNS resolver cache
netstat -anoconnections and listening ports with the owning PID (-a all, -n numeric, -o PID)
netstat -ano | findstr :8080which PID uses port 8080; then tasklist /fi "pid eq PID"
tasklist / tasklist /svcrunning processes / services hosted in each process
systeminfoOS version and build, install date, boot time, memory, hotfixes

Sources