Programming

What is batch file?

A batch file is a plain-text Windows script with a .bat or .cmd extension that cmd.exe runs line by line to automate Command Prompt tasks.

What is a batch file?

Batch files date back to MS-DOS and still run on every current version of Windows, because the command interpreter, cmd.exe, ships with the operating system. A batch file is a list of the same commands you could type at a Command Prompt, such as copy, del, net use, ipconfig and robocopy, plus a small set of scripting features: variables, conditions, loops, labels and jumps. The .cmd extension arrived with Windows NT and behaves almost the same as .bat. Double-clicking either one runs it, which is convenient for users and also a risk when the file arrived by email or from a website.

Basic syntax

  • @echo off stops cmd.exe from printing each command before running it. The @ hides that first line too.
  • REM or :: starts a comment.
  • set SHARE=\\fs01\public creates a variable, and %SHARE% reads it. Built-in environment variables work the same way, for example %USERNAME%, %COMPUTERNAME% and %USERPROFILE%. set /p NAME=Enter a name: prompts the user for a value.
  • if exist Z:\ net use Z: /delete runs a command only when a condition is true. if errorlevel 1 checks whether the previous command failed.
  • for %%f in (*.log) do del "%%f" loops over matching files. Inside a batch file the loop variable takes two percent signs; typed at the prompt, it takes one.
  • goto end jumps to a label written as :end, and call other.bat runs another batch file and returns.
  • pause waits for a key press so the window does not close before the user reads the output.
  • exit /b 1 ends the script and returns an exit code to whatever started it.

Typical uses

  • Mapping network drives and printers at sign-in as part of a login script
  • Clearing temp folders or old log files
  • Copying files or simple backups with robocopy or xcopy, run on a schedule through Task Scheduler
  • Collecting system details (hostname, ipconfig /all, systeminfo) into a text file to attach to a ticket
  • Running a fixed sequence of repair commands, such as sfc and DISM, from an elevated prompt
  • Launching an application with specific options

Batch vs PowerShell

FeatureBatch (.bat, .cmd)PowerShell (.ps1)
Interpretercmd.exepowershell.exe or pwsh.exe
Works withTextObjects
Variablesset name=value, read as %name%$name = "value"
Running a script fileRuns when double-clicked or calledControlled by execution policy, which blocks scripts by default on Windows client editions; double-clicking opens the file in Notepad
Best forQuick, simple tasks and legacy scriptsModern Windows, Active Directory and Microsoft 365 administration, complex logic

Risks and good habits

A batch file runs with the rights of whoever starts it, and "Run as administrator" gives it full control of the PC. A one-line mistake, such as del /s /q pointed at the wrong folder, can wipe data, and a runaway loop can fill a disk or restart machines unexpectedly. Attackers have long sent .bat and .cmd files as email attachments or used them to launch malware. Read a script before running it, test it on a spare PC or virtual machine first, never hard-code passwords, and keep production scripts in a location users cannot edit.

Batch file on the CompTIA A+ exam

Core 2 (220-1202) objective 4.8 covers script file types, use cases and risks. Match .bat (and .cmd) to Windows batch run by cmd.exe, .ps1 to PowerShell, .vbs to VBScript, .sh to Linux and macOS shells, .js to JavaScript and .py to Python. If a question asks for the simplest built-in way to automate Command Prompt commands on any Windows PC, think batch file. If you see %VARIABLE% syntax or @echo off, it is batch. If the task calls for modern Windows administration with cmdlets and objects, think PowerShell.

Studying for CompTIA (Programming)?

ExamWizardz turns the official objectives into a guided study plan — with practice tests, realistic PBQs, and a readiness score. Start free.

Start studying free

Related terms