Batch File Extensions

Maintained on
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.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>

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
OriginSince MS-DOSIntroduced with Windows NT
Program that runs itcmd.execmd.exe
Basic syntaxAlmost the sameAlmost the same
ERRORLEVEL after some commands succeedMay stay unchangedAlways 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.

Note for old operating systems

.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.exe script → .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.

Practice Quiz

Files containing commands that run on Command Prompt can have two types of extensions: .bat and .cmd. About the differences between them, select the statement that is incorrect from the following options.

#Command Prompt #Batch File #Extension #bat #cmd