CMD Commands

Command Dispatcher Pattern

 1@echo off
 2setlocal
 3
 4set "command=%~1"
 5
 6if "%command%"=="start" goto :start
 7if "%command%"=="stop" goto :stop
 8if "%command%"=="restart" goto :restart
 9if "%command%"=="status" goto :status
10goto :usage
11
12:start
13echo Starting service...
14rem Your start logic here
15goto :end
16
17:stop
18echo Stopping service...
19rem Your stop logic here
20goto :end
21
22:restart
23call :stop
24call :start
25goto :end
26
27:status
28echo Checking status...
29rem Your status logic here
30goto :end
31
32:usage
33echo Usage: %~nx0 {start^|stop^|restart^|status}
34exit /b 1
35
36:end
37endlocal

Useful Commands

File Operations

 1rem Copy files
 2copy source.txt dest.txt
 3xcopy /E /I source_dir dest_dir
 4
 5rem Move files
 6move file.txt newlocation\
 7
 8rem Delete files
 9del file.txt
10del /Q /S *.tmp
11
12rem Create directory
13mkdir path\to\directory
14md nested\directories
15
16rem Remove directory
17rmdir /S /Q directory
18
19rem List files
20dir
21dir /S /B *.txt
22
23rem Find files
24where /R C:\ filename.txt
25
26rem File attributes
27attrib +R file.txt
28attrib -H -S file.txt

Text Processing

 1rem Display file
 2type file.txt
 3
 4rem Find string
 5find "text" file.txt
 6findstr /I "pattern" *.txt
 7
 8rem Sort
 9sort file.txt
10
11rem More (paginate)
12type file.txt | more
13
14rem Count lines
15find /C /V "" file.txt

Process Management

 1rem List processes
 2tasklist
 3
 4rem Find process
 5tasklist | findstr "notepad"
 6
 7rem Kill process
 8taskkill /IM notepad.exe /F
 9taskkill /PID 1234 /F
10
11rem Start process
12start notepad.exe
13start "" "C:\Program Files\App\app.exe"
14
15rem Run elevated
16runas /user:Administrator cmd

Network Commands

 1rem Ping
 2ping google.com
 3ping -n 10 192.168.1.1
 4
 5rem Trace route
 6tracert google.com
 7
 8rem DNS lookup
 9nslookup google.com
10
11rem IP configuration
12ipconfig
13ipconfig /all
14ipconfig /flushdns
15
16rem Network connections
17netstat -ano
18netstat -ano | findstr :80
19
20rem Test port
21telnet hostname 80

System Information

 1rem System info
 2systeminfo
 3
 4rem Computer name
 5hostname
 6
 7rem Windows version
 8ver
 9
10rem Environment variables
11set
12echo %PATH%
13echo %USERPROFILE%
14
15rem Disk info
16wmic logicaldisk get name,size,freespace
17
18rem CPU info
19wmic cpu get name,numberofcores
20
21rem Memory info
22wmic memorychip get capacity
23
24rem Services
25sc query
26sc query state= all
27net start

Scripting Techniques

Variables

 1rem Set variable
 2set VAR=value
 3set /A NUM=10+5
 4
 5rem User input
 6set /P NAME=Enter name: 
 7
 8rem Command output
 9for /F %%i in ('date /t') do set TODAY=%%i
10
11rem Environment variables
12echo %COMPUTERNAME%
13echo %USERNAME%
14echo %CD%

Conditionals

 1rem If statement
 2if exist file.txt (
 3    echo File exists
 4) else (
 5    echo File not found
 6)
 7
 8rem String comparison
 9if "%VAR%"=="value" (
10    echo Match
11)
12
13rem Numeric comparison
14if %NUM% GTR 10 (
15    echo Greater than 10
16)
17
18rem Check error level
19command
20if errorlevel 1 (
21    echo Command failed
22)
23
24rem Check if variable is defined
25if defined VAR (
26    echo Variable is set
27)

Loops

 1rem For loop (files)
 2for %%F in (*.txt) do (
 3    echo %%F
 4)
 5
 6rem For loop (numbers)
 7for /L %%N in (1,1,10) do (
 8    echo %%N
 9)
10
11rem For loop (directories)
12for /D %%D in (*) do (
13    echo %%D
14)
15
16rem For loop (command output)
17for /F %%i in ('dir /B') do (
18    echo %%i
19)
20
21rem Recursive
22for /R %%F in (*.log) do (
23    del "%%F"
24)

Functions (Subroutines)

1@echo off
2
3call :function arg1 arg2
4goto :eof
5
6:function
7echo Arg1: %~1
8echo Arg2: %~2
9exit /b 0

One-Liners

 1rem Backup with timestamp
 2for /F "tokens=1-4 delims=/ " %%a in ('date /t') do set DATE=%%c%%a%%b
 3copy file.txt file.txt.%DATE%
 4
 5rem Find large files
 6forfiles /S /C "cmd /c if @fsize gtr 104857600 echo @path @fsize"
 7
 8rem Kill process by port
 9for /f "tokens=5" %a in ('netstat -ano ^| findstr :8080') do taskkill /F /PID %a
10
11rem Batch rename
12for %f in (*.txt) do ren "%f" "prefix_%f"
13
14rem Create multiple directories
15for /L %i in (1,1,10) do mkdir folder%i

Further Reading

Related Snippets