Updated: October 2026
A .bat file is a plain text file of Windows commands that runs top to bottom when you open it. Technicians use them for small repeatable jobs: mapping a drive, clearing a cache, copying a log folder. This guide shows how to create and run a .BAT file, how to run it as administrator, and where PowerShell takes over.
What a .BAT File Is
A .bat file, also called a batch file, is a text file that the Windows command interpreter, cmd.exe, reads one line at a time. Each line is a command you could type at a Command Prompt. The file saves the typing and runs the same steps every time.
A .cmd file does the same job in the same interpreter. Microsoft's call command accepts either extension, so the two can call each other.
The detail that trips people up is the language. A .bat file speaks CMD, not PowerShell. In a 2025 r/sysadmin thread, a technician's .bat file stopped at a PowerShell line that worked when pasted into a terminal. The reply: .bat and .cmd files run under CMD, and the terminal defaults to PowerShell.
Our comparison of PowerShell vs CMD covers the language gap in full.
How to Create a .BAT File
You need Notepad and nothing else.
- Open Notepad and type one command per line.
- Choose File, then Save As.
- Set "Save as type" to All files.
- Name the file with the .bat extension, for example
copylogs.bat, and save it somewhere short such asC:\Scripts.
Here is a starter script:
bat@echo off echo Copying logs... mkdir C:\Temp\Logs 2>nul xcopy "C:\Logs\App" "C:\Temp\Logs" /E /I /Y echo Done. pause
The step people miss is the second one. If "Save as type" stays on Text Documents, Notepad saves copylogs.bat.txt, and a double-click just opens Notepad. Turn on File name extensions in Explorer (View, Show) to see what you really saved.
The Commands Worth Knowing
Batch syntax is small. These do most of the work in a technician's scripts.
| Command | What it does |
|---|---|
@echo off | Stops each line printing before it runs |
echo text | Prints a message |
set NAME=value | Stores a value, read back as %NAME% |
if / if errorlevel 1 | Branches on a test or on the last command's exit code |
goto :label | Jumps to a label; goto :eof exits the script |
call other.bat | Runs another batch file, then returns |
setlocal | Keeps variable changes inside this script |
%~dp0 | The drive and folder the script sits in |
pause | Waits for a key press |
exit /b 1 | Ends the script with an exit code |
Arguments are simple too. Whatever you type after the file name arrives as %1, %2 and so on, and %* holds all of them, so copylogs.bat D:\Archive can pass a destination into the script. Wrap %1 as "%~1" to strip stray quotes and keep paths with spaces intact.
A short example ties them together. It checks that a server answers before mapping a drive:
bat@echo off ping -n 1 fileserver >nul if errorlevel 1 ( echo Server unreachable. Check the VPN. exit /b 1 ) net use Z: \\fileserver\shared
The same net use line, with the Group Policy alternative, is in our guide to map a network drive on Windows 11.
How to Run a .BAT File
There are four common ways, and each has a different use.
- Double-click. Fastest, but the window closes when the script ends. Add
pauseas the last line to read the output. - From Command Prompt. Open it,
cdto the folder, and type the file name, or give the full path in quotes. The window stays open, so errors stay on screen. Use this to debug. - From PowerShell. Type
.\copylogs.bat. PowerShell will not run a file from the current folder without the.\prefix. - Task Scheduler or a GPO startup script. Runs unattended, which changes whose rights apply.
That last one catches people. In a December 2025 r/sysadmin thread, a batch job that copied files between servers worked as the logged-on user and did nothing as a dedicated service account. Running the script from a command line as that account exposed two permission gaps, on the log file and on the share.
Two safety notes. Right-click and choose Edit to read a .bat file from someone else before you run it, because it runs with your rights. And PowerShell's execution policy only governs PowerShell scripts, so it does not block or allow a .bat file.
How to Run a .BAT File as Administrator
A batch file has no admin setting of its own. It inherits the rights of whatever starts it. Right-click the file and choose Run as administrator; on Windows 11 this can sit under Show more options. If you run the file often, make a shortcut, open Properties, then Advanced, and tick Run as administrator. For a scheduled run, tick Run with highest privileges in Task Scheduler. You can also open Command Prompt as administrator and start the file from there.
The trap is the working folder. An elevated Command Prompt opened from the Start menu lands in C:\Windows\System32, so a script that uses relative paths now looks in the wrong place. Start the script with cd /d "%~dp0" to move into the folder the file lives in.
A guard at the top also saves a confusing failure. net session only succeeds in an elevated window, so it works as a quick check:
batnet session >nul 2>&1 if errorlevel 1 ( echo Run this file as administrator. pause exit /b 1 )
Why a .BAT File Fails
Five causes account for the usual failures.
The window closes before you can read it. The script ran and finished. Add pause, or run it from an open Command Prompt.
"Is not recognized as an internal or external command." The program is not on the PATH, or a path with spaces has no quotes. Put "C:\Program Files\Tool\tool.exe" in quotes and give the full path.
The file is really a .txt. Check the extension first. Explorer hides it by default.
A UNC path will not cd. Command Prompt cannot change into \\server\share. Use pushd \\server\share, which maps a temporary drive letter, and popd to release it.
It works for you and fails for the scheduler. Rights, mapped drives and the working folder all differ for a service account or SYSTEM. Mapped drives belong to a user session, so use full UNC paths in anything unattended.
When to Move From .BAT to PowerShell
Batch is the right size for a sequence of commands. It strains when the job needs data. Microsoft's own cmd reference points people who want more advanced scripting toward PowerShell.
Three signs it is time to switch. The script parses output, such as a CSV or JSON, rather than just printing it. It needs real error handling instead of checking errorlevel after every line. Or it has to run against other computers rather than the one you are sitting at. Our PowerShell commands guide lists the cmdlets to start with.
The same idea exists outside Windows. A bash script plays the role a batch file plays here, and our what is bash guide shows it for admins who work on both.
Picking the Right Tool for the Job
Match the tool to the size of the task. A one-off on one PC is a .bat file. Something that repeats goes in Task Scheduler, with the script writing a log. Anything that handles data or needs error handling is PowerShell. A job that has to reach every device a client owns needs something that can run the script on all of them and bring the output back, which is what OpenFrame does for scripts across a client's devices.
Watch the short version of all this in a walkthrough that creates and runs a batch file from scratch:
.BAT Files, in Short
A .bat file is a text file of CMD commands, saved with the right extension and run from a double-click, a command line or a scheduler. Add pause while you test, move to the script's own folder with %~dp0 when it runs elevated, and move to PowerShell when the job outgrows a list of commands. For a wider list of everyday commands to put inside one, see our CMD commands guide.

"Fae" Grace Meadows
Lead AI Fairy
Some things defy easy explanation: magic dust, the northern lights… and Flamingo’s AI Angels. Think Charlie’s Angels, reimagined with automation brains and serious RMM (Remote Monitoring & Management) chops. Weird? A little. Effective? Absolutely. That’s the job.
