Batch File Extensions
Table of Contents
By using Command Prompt, you can run operations on your computer from the command line.
You can type commands directly, but to run several commands together it is effective to create a batch file.
Write a series of commands in a file and run it by double-clicking it or from Command Prompt, and all of the commands run in one go.
Batch files use one of two extensions: .bat or .cmd. Both are common in current versions of Windows, but they differ slightly in origin and in the environment they were designed for.
About batch files
Files with either the .bat or .cmd extension contain commands that run on Command Prompt.
When you start one by double-clicking it, Command Prompt launches and the commands are executed in one go.
In many cases, a window like the one below appears briefly and then closes. This indicates that the batch file has finished running.
Unless the file contains a command that deliberately waits for user input, this is the usual behavior.
Differences between .bat and .cmd
.bat has been used since the MS-DOS era, and .cmd was introduced with the Windows NT family.
On Windows 10 / 11, both are normally run by cmd.exe, and the basic batch syntax is almost identical.
| Item | .bat | .cmd |
|---|---|---|
| Origin | Since MS-DOS | Introduced with Windows NT |
| Program that runs it | cmd.exe | cmd.exe |
| Basic syntax | Almost the same | Almost the same |
| ERRORLEVEL after some commands succeed | May stay unchanged | Always updated |
The behavioral difference is ERRORLEVEL
The best-known behavioral difference is how the exit code (ERRORLEVEL) is handled.
For some commands such as PATH, SET, ASSOC and FTYPE, a .cmd file updates ERRORLEVEL (to 0) even on success, whereas in a .bat file the previous value may remain after a successful run.
In a batch file that checks ERRORLEVEL to detect errors, this difference can change the behavior. See the related article on ERRORLEVEL for details.
.cmd is an extension of the Windows NT family, so it cannot be run on MS-DOS or on Windows
95/98/Me. If you only target current versions of Windows, you can ignore this.
Which should you use?
If you are unsure, choose by the following criteria:
- Only for current Windows, and you want to make clear it is a
cmd.exescript →.cmd - Your existing operations or documents consistently use
.bat→.bat
How to change the extension
To use a .txt file saved in Notepad as a batch file, change its extension.
In File Explorer, enable “File name extensions” under “View”, then rename the file to sample.bat or sample.cmd. If extensions are hidden, the real file name may end up as sample.bat.txt, so be careful.
For the full creation steps, see the related article on creating batch files. For garbled text when saving batch files, see the related article on character encoding.
Running a batch file without the extension
Both .bat and .cmd are in the default value of the PATHEXT environment variable. In Command Prompt you can therefore type just sample, and sample.bat or sample.cmd is run.
Note that PowerShell scripts use the .ps1 extension and are a different thing from batch files.