Table of Contents
- Part 1: CMD Fundamentals
- Command Prompt Interface
- Commands and Variables
- Advanced Commands
- Batch Processing Basics
- Practical Solutions
- Part 2: Batch Scripting
- Batch Processing Concepts
- Essential Batch Commands
- Working Examples
Part 1: CMD Fundamentals
Command Prompt Interface
Accessing the command line:
* Win+R → type cmd → press Enter
* Start Menu → right-click Command Prompt → Run as administrator
Commands and Variables
Internal Commands
Built-in commands that are always available:
* dir → list directory contents
* cd → change directory
- Tab key: auto-complete and cycle through filenames
* mkdir → create directory
* rmdir → remove directory
* shutdown → shutdown command (dash and forward slash work identically)
* Copy/paste in CMD: right-click title bar → Edit → Mark/Copy
External Commands
Located in C:\Windows\System32:
* notepad → text editor
* iexpress → executable bundler
* mspaint → graphics tool
Modifying External Commands
* Rename ping.exe to myping.exe to create a custom command
Adding Commands via Environment Variables
* Right-click This PC → Properties → Advanced System Settings → Environment Variables → edit PATH
Advanced Commands
Network-Related Commands
* ping → test network connectivity
- -t → continuous ping, Ctrl+C to stop
* ipconfig → display IP configuration
- /all → show detailed information
* netstat → view active connections
* pathping → route tracing utility
Account and Permission Commands
* net user <username> <password> /add → create new user
* net localgroup administrators <username> /add → add user to administrators group
Port and Service Management
* net start telnet → start telnet service
* Win+R → mstsc → Remote Desktop Connection (GUI-based)
* net user <username> /del → delete user
Additional Privilege Operations
* net localgroup "Remote Desktop Users" <username> /add → enable remote desktop access
* REG ADD HKLM\SYSTEM\CurrentControlSet\Control\Terminal" "Server /v fDenyTSConnections /t REG_DWORD /d 0 /f → enable RDP port 3389
Batch Processing Basics
What is Batch Processing?
Creating a batch file:
* Create new text file → rename with .bat or .cmd extension (avoid conflicts with existing commands)
* Add content: ping www.google.com
* Double-click to execute
Notes:
.batfiles run in both DOS and CMD.cmdfiles only run in CMD- Right-click any batch file to edit its contents
Simple Script Examples
Registry modification for homepage change:
reg add "HKCU\Software\Microsoft\Internet Explorer\Main" /v "Start Page" /d "http://example.com" /f
Endless popup loop:
:loop
start cmd.exe
goto loop
File concatenation:
copy /b file1.txt+image.jpg output.jpg
File bundling:
iexpress
Practical Solutions
* netsh winsock reset → fixes auto-closing CMD window issue by resetting network adapter configuration
* net user administrator /active:yes → reactivate disabled built-in administrator account
* convert D:/FS:NTFS → convert FAT32 drive to NTFS without data loss
Fixing unformattable USB drives:
diskpart
list disk
select disk 3
clean
Part 2: Batch Scripting
Batch Processing Concepts
Overview
Batch prcoessing automates the execution of multiple commands without user interaction. This approach is also known as workload automation and job scheduling.
A batch file is a text file ending in .bat or .cmd where each line contains a command executed by the command interpreter. Any text editor can create and modify these files.
Key Characteristics
- Efficiency: Automation eliminates manual intervention, reducing operatoinal costs and increasing processing speed. Organizations can prioritize data processing sequences as needed.
- Accuracy: Removing human involvement prevents errors, saving time and resources while delivering more reliable results.
- Simplicity: Once configured, batch files require no ongoing support or specialized software. The low barrier to entry makes this an accessible solution for many tasks.
- Offline Operation: Batch systems execute independently. Machines continue processing after hours, and administrators control timing to prevent system overload.
Essential Batch Commands
Syntax Rules
- Batch files are simple programs using
if,goto, andforconstructs for flow control. - A batch file functions like a DOS external command—add its directory to PATH to run it from anywhere.
- Case-insensitive (command identifiers ignore case).
- File extensions:
.bator.cmd. - Execute by typing the filename at the command prompt or double-clicking the file.
System Variables
| Variable | Value |
|---|---|
%SystemRoot% or %windir% |
C:\WINDOWS |
%ProgramFiles% |
C:\Program Files |
%OS% |
Windows_NT |
%TEMP% or %TEM% |
C:\DOCUME~1\ADMIN~1\LOCALS~1\Temp |
Working Examples
Basic Batch File Structure
@echo off
:: Display current echo status
echo
:: Comments using rem
rem This is a comment
:: Prevent window from closing
pause
Displaying System Variables
@echo off
echo %SystemRoot%
echo %windir%
echo %OS%
echo %TEMP%
pause
Comprehensive Command Reference
@echo off
:: ===== HELP AND SET COMMANDS =====
:: Use /? for help on any command
:: type /? >> help.txt
:: help type
:: SET command: define, display, or remove variables
:: set myvar=hello
:: echo %myvar%
:: set /a result=5+3
:: echo %result%
:: ===== ECHO COMMAND =====
:: echo [on/off] → toggle command display
:: echo → show current echo setting
:: echo [message] → display text
:: echo Hello World
:: ===== GOTO COMMAND =====
:: goto label → jump to specified label
:: Labels are defined as :labelName
:retry
:: echo Insert disk and press any key...
:: goto retry
:: ===== PAUSE COMMAND =====
:: Pauses execution and shows "Press any key to continue..."
:: pause
:: ===== CALL COMMAND =====
:: Call another batch file without terminating parent
:: call secondary.bat
:: call "C:\Program Files\App\launcher.exe"
::
:: Use CALL for invoking batch files and waiting for completion.
:: Use START for launching external applications asynchronously.
:: ===== START COMMAND =====
:: Launch external programs
:: start another.bat
:: start "" "C:\Program Files\Tool\app.exe"
:: ===== COPY COMMAND =====
:: Copy files or directories
:: copy source.txt destination\
:: ===== IF CONDITIONAL =====
:: Execute commands based on conditions
:: Syntax: if [not] "string1" == "string2" command
set appPath=C:\MyApp
set config=default
set /a configNum=config
if %configNum% neq %config% (
copy /Y "config.dll" "%appPath%\lib\config.dll"
)
:: %~dp0 automatically expands to the directory containing the batch file
:: ===== FOR LOOP =====
:: Iterate through ranges, files, or command output
:: Get serial ports from external tool
set appDir=C:\MyApp
for /f "delims=" %%p in ('"%appDir%\GetPorts.exe" 0') do set portList=%%p
echo %portList%
:: ===== VARIABLE OPERATIONS =====
:: /a → evaluate as arithmetic expression
:: /b → iterate folders only in current directory
:: /f → parse lines from files or command output
:: /d → inherit current environment in new window
set text=10+5
echo %text%
set /a calc=10+5
echo %calc%
set /P count=Enter quantity:
:: ===== CHOICE COMMAND =====
:: Prompt user for selection
:menu
choice /c YNC /m "Y=Yes, N=No, C=Cancel" /T 5 /D N
if errorlevel 3 goto QUIT
if errorlevel 2 goto DECLINE
if errorlevel 1 goto CONFIRM
:CONFIRM
echo You selected YES
goto done
:DECLINE
echo You selected NO
goto done
:QUIT
echo You selected CANCEL
:done
:: ===== TASKLIST COMMAND =====
:: List running processes
tasklist
:: Filter for specific process
tasklist | find /i "notepad.exe" || start notepad
pause
Handling Batch Parameters
@echo off
:: %0 → batch filename itself
:: %1 → first argument
:: %* → all arguments
echo First argument: %1
echo All arguments: %*
pause
Usage:
script.bat arg1 arg2 arg3