How to Create a Batch File in Windows 10
In this article
Batch files are plain-text scripts that run a series of commands in the Windows command prompt. They are useful for automating repetitive tasks like file cleanup, launching multiple programs, or setting up a development environment. You don't need any special software—just Notepad and a basic understanding of command-line commands. This guide walks you through creating, saving, and running your first batch file, plus common pitfalls and fixes.
What You'll Need
- A Windows 10 PC with any edition (Home, Pro, etc.)
- Notepad (built-in) or any text editor like Notepad++ or VS Code
- Basic knowledge of commands like
echo,pause, anddir(not required but helpful)
Step-by-Step
- Open Notepad. Press Win + R, type
notepad, and press Enter. Alternatively, search for "Notepad" in the Start menu. - Write your commands. Each line in Notepad will be executed as a separate command. For a simple test, type:
@echo off echo Hello, World! pause
@echo offhides the command text so only the output is shown.echoprints text to the screen.pausekeeps the window open so you can see the result.
- Save the file with a .bat extension. Click File > Save As. In the "Save as type" dropdown, choose All Files (*.*). Name your file something like
hello.bat(the extension must be.bator.cmd). Choose a location you can find, like the Desktop. Click Save. - Run the batch file. Double-click the saved file. A command prompt window should open, display "Hello, World!", and wait for you to press a key. Press any key to close it.
- Edit and iterate. Right-click the .bat file and select Edit to open it in Notepad again. Make changes, save, and run again.
- Add more advanced commands. You can include commands like
startto launch programs,mkdirto create folders, orcopyto move files. For example:@echo off mkdir C:\Backup copy C:\important.txt C:\Backup\ echo Done! pause
Common Mistakes
- Forgetting the .bat extension. If you save as
hello.txt, it won't run as a batch file. Always use.bator.cmd. - Using Word or other rich text editors. They add formatting that confuses the command prompt. Stick to Notepad or a plain-text editor.
- Not using
@echo off. Without it, every command is echoed to the screen, making output messy. - Spaces in file paths. If a path has spaces (e.g.,
C:\Program Files), you must enclose it in quotes:start "" "C:\Program Files\MyApp.exe". - Running from the wrong directory. Batch files execute from the current working directory, which may not be where the file is stored. Use
cd /d %~dp0to change to the script's directory.
Tips
- Test with
pause. Always addpauseat the end while testing so you can see errors before the window closes. - Use comments. Add
REMor::at the start of a line to add notes. Example:REM This is a comment. - Run as administrator. If your script needs admin rights, right-click the file and select Run as administrator.
- Create a shortcut. You can make a shortcut to your .bat file and assign a keyboard shortcut to run it quickly.
- Use environment variables.
%USERPROFILE%points to your user folder,%WINDIR%to the Windows folder. This makes scripts portable.
Troubleshooting
- The window closes immediately. This usually means an error occurred. Add
pauseas the last line to keep the window open and read the error message. If you already havepause, check the command syntax. - "'xxx' is not recognized as an internal or external command" means the command you typed doesn't exist or isn't in the PATH. Double-check spelling and make sure the program is installed.
- The file opens in Notepad instead of running. This happens if the file extension is actually .txt. Check the file name in File Explorer—if it shows
hello.bat.txt, rename it tohello.bat. - Permission denied errors. If you're writing to a protected folder like
C:\Windows, run the batch file as administrator. - Special characters like
>or|cause issues. These are reserved by the command prompt. To print them, escape with^(e.g.,echo ^>) or use quotes.
Frequently Asked Questions
Can I convert a .bat file back to .txt?
Yes, just rename the file to have a .txt extension. The content will remain the same, but it will no longer execute.
How do I make a batch file run automatically at startup?
Place a shortcut to the .bat file in the Startup folder. Press Win + R, type shell:startup, and press Enter. Copy a shortcut there.
Can I use PowerShell commands in a batch file?
Not directly. You can call PowerShell from a batch file using powershell -Command "...", but the batch file itself uses cmd syntax.
Why does my batch file say "Access is denied"?
This often happens when the script tries to modify files or folders that require administrator privileges. Right-click the file and choose "Run as administrator."
How do I schedule a batch file to run at a specific time?
Use Task Scheduler. Open it from the Start menu, click "Create Basic Task," and follow the wizard to point to your .bat file.
What is the difference between .bat and .cmd?
Both are batch files and work almost identically. .cmd is newer and was introduced with Windows NT, but for most purposes they are interchangeable.