Beginner's Guide to Windows Batch Scripting (Expanded Edition)
This guide explains essential Windows batch (.bat) commands and gives practical examples.
------------------------------------------------------------
1. ECHO
Usage: echo Hello
- Prints text to the screen.
Example:
echo Welcome to my script!
echo.
echo Starting system check...
2. SET
Usage: set name=value
- Stores a variable.
Example:
set logFile=C:\Users\User\Desktop\log.txt
echo Log will be saved to %logFile%
3. REM or :: (Comments)
Usage: rem Notes here or :: Notes here
- Adds internal notes or disables lines.
Example:
rem This script checks for updates
4. IF
Usage: if EXIST "file.txt" echo Found file!
- Runs a command only if a condition is met.
Example:
if EXIST "log.txt" del log.txt
5. GOTO
Usage: goto label
- Jumps to a label in the script.
Example:
goto start
:start
echo Running process...
6. PAUSE
Usage: pause
- Freezes script until user presses a key.
Example:
pause
7. TIMEOUT
Usage: timeout /t 10 /nobreak
- Waits before continuing.
Example:
echo Waiting 5 seconds...
timeout /t 5
8. SHUTDOWN
Usage: shutdown /s /t 60
- Schedules system shutdown.
Examples:
shutdown /s /t 0 (shutdown now)
shutdown /r /t 30 (restart in 30s)
9. START
Usage: start [program or file]
Examples:
start notepad.exe
start https://google.com
10. FINDSTR
Usage: findstr "text" filename.txt
Example:
findstr /c:"[SR]" C:\Windows\Logs\CBS\CBS.log > sfc_results.txt
11. REDIRECTION
- > overwrites file, >> appends to file.
Examples:
echo Start > output.txt
echo More >> output.txt
12. EXIT
Usage: exit
- Ends the batch file.
------------------------------------------------------------
BONUS: Sample Script
@echo off
echo Running System Check...
sfc /scannow >> %userprofile%\Desktop\scan_log.txt
echo Done. PC will shut down.
shutdown /s /t 60
------------------------------------------------------------
TIPS:
- Always test scripts in a safe environment.
- Use Notepad to create .bat files.
- Right-click > Run as Administrator when needed.