For those who don't know, a Batch file is a text file containing a series of commands to be executed by Command Prompt (CMD). When a batch file is run, CMD reads the file and executes its commands. To open your CMD, click START RUN type 'CMD' OK (some of you may need to type 'CMD.EXE'). Batch File Basics A batch file is simply a text file saved with the.bat file extension. You can write one using Notepad or a more advanced text editor like Notepad, but don't use a word processor like Microsoft Word. Let's create a simple batch file. A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions. Basic Batch File Commands We are done with all the basic preliminary concepts like variables, operators, conditional, branching and looping commands. Now this tutorial deals with the basic batch file commands that are used generally to make a batch file more complicated. Sub commands, Switches, Parameters.
© Provided by Windows CentralYou can use batch files to automate tasks on Windows 10, and in this guide, we'll show how.
On Windows 10, a batch file is a special text file that typically has a .bat extension, and it includes one or more commands that Command Prompt can understand and run in sequence to perform various actions.
Usually, you can type commands manually to perform a particular task or change system settings on Windows 10. However, a batch file simplifies the work of retyping commands, saving you time and potentially irreversible mistakes.
You can also use other tools like PowerShell to write even more advanced scripts. However, using batch files with Command Prompt is a convenient option when you need to run commands to change settings, automate routines, and start apps or launch websites.
In this Windows 10 guide, we'll walk you through the steps to create and run your first batch file on your device. Also, we'll outline the steps to create advanced scripts and automate scripts using the Task Scheduler.
- How to create batch file on Windows 10
- How to run batch file on Windows 10
How to create a batch file on Windows 10
The process of creating a batch (script or batch script) file is simple. You only need a text editor and some basic knowledge typing Command Prompt native commands. In the instructions below, we'll outline the steps for writing a basic and advanced batch file, as well as the steps to write a script to change the system settings on Windows 10.
Create basic batch file
To create a basic batch file on Windows 10, use these steps:
- Open Start.
- Search for Notepad and click the top result to open the app.
Type the following lines in the text file to create a batch file:
@ECHO OFF ECHO Congratulations! Your first batch file executed successfully. PAUSE © Provided by Windows CentralThe above script outputs the phrase, 'Congratulations! Your first batch file executed successfully' on the terminal screen.
© Provided by Windows Central- @ECHO OFF — Disables the display prompt to show only the message on a clean line. Usually, this line goes at the beginning of the file. (You can use this command without '@,' but the symbol hides the command being executed to create a cleaner return.)
- ECHO — Prints any text on the screen.
- PAUSE — Keeps the window open after executing the command. If you don't use this command, the window will close automatically as soon as the script finishes running. You can use this command at the end of the script or after a specific command when running multiple tasks, and you want to pause between them.
- Click the File menu.
- Select the Save as option.
Type a name for the script — for example, first_basic_batch.bat.
Quick note: While batch files typically use the .bat file extensions, you can also find scripts using the .cmd or .btm file extensions.
Once you complete the steps, you can double-click the file to run it, or you can use the steps below to learn the different ways to execute a batch file on Windows 10.
Create advanced batch file
To create an advanced batch file to execute multiple commands, use these steps:
- Open Start.
- Search for Notepad and click the top result to open the app.
Type the following lines in the text file to create a more advanced batch file:
@ECHO OFF :: This batch file details Windows 10, hardware, and networking configuration. TITLE My System Info ECHO Please wait.. Checking system information. :: Section 1: Windows 10 information ECHO ECHO WINDOWS INFO ECHO systeminfo | findstr /c:'OS Name' systeminfo | findstr /c:'OS Version' systeminfo | findstr /c:'System Type' :: Section 2: Hardware information. ECHO ECHO HARDWARE INFO ECHO systeminfo | findstr /c:'Total Physical Memory' wmic cpu get name wmic diskdrive get name,model,size wmic path win32_videocontroller get name :: Section 3: Networking information. ECHO ECHO NETWORK INFO ECHO ipconfig | findstr IPv4 ipconfig | findstr IPv6 START https://support.microsoft.com/en-us/windows/windows-10-system-requirements-6d4e9a79-66bf-7950-467c-795cf0386715 PAUSE © Provided by Windows CentralThe above script runs a series of commands to query different system information. Then it groups them into three different categories, including 'WINDOWS INFO,' 'HARDWARE INFO,' and 'NETWORK INFO.' The 'start' command will also open a Microsoft support website listing the official Windows 10 system requirements on your default web browser, which you can check against your information.
© Provided by Windows Central- @ECHO OFF — Disables the display prompt to show only the message on a clean line. Usually, this line goes at the beginning of the file. (You can use this command without '@,' but the symbol hides the command being executed for a cleaner return.)
- TITLE — Displays a custom name in the title bar of the window.
- :: — Allows you to write comments and documentation information. These details are ignored when the batch file runs.
- ECHO — Prints the exact text on the screen.
- START — Lets you launch an app or website with the default web browser.
- PAUSE — Keeps the window open after executing the command. If you don't use this command, the window will close automatically as soon as the script finishes running. You can use this command at the end of the script or after a specific command when running multiple tasks, and you want to pause between them.
- Click the File menu.
- Select the Save as option.
- Type a name for the script — for example, first_advanced_batch.bat.
After you complete the steps, double-click the .bat file to run it, or you can use the steps below to learn the different ways to run a batch.
Create actionable batch file
In addition to executing and displaying content in a Command Prompt window, you can also write non-interactive batch scripts to perform virtually any task you need.
Dbt diary cardsdialectical behavioral training. To create a batch file that runs a specific command without user interaction, use these steps:
- Open Start.
- Search for Notepad and click the top result to open the app.
Copy and paste the following command in the text file:
net use z: PATH-NETWORK-SHAREFOLDER-NAME /user:YOUR-USERNAME YOUR-PASSWORD © Provided by Windows CentralQuick note: In the screenshot, you'll notice the 'pause' command, but it's not required. It was added in this example to take a screenshot of the terminal. If you're accessing the files from another computer that uses a specific username and password, don't forget to use the /user: option with the necessary credentials.
The above script includes a simple command to map a network folder as a drive inside File Explorer using the 'Z' drive letter.
© Provided by Windows Central- Click the File menu.
- Select the Save as option.
- Type a name for the script — for example, mount-z-network-drive.bat.
Once you complete the steps, the batch file will map the network folder with the specified settings without opening a Command Prompt window. While we only used one command in this particular file, you can include as many commands as you like, as long as you write them one per line.
How to run a batch file on Windows 10
On Windows 10, you can run a batch file in at least three ways. You can run it on-demand using File Explorer or Command Prompt. You can create a task with Task Scheduler to run it on schedule. Or you can place the script in the 'Startup' folder to run it every time you sign in to your Windows 10 account.
Run batch file on-demand
If you need to run a script on-demand, you have two choices, including Command Prompt or File Explorer.
Command Prompt
To run a batch file with Command Prompt, use these steps.
- Open Start.
- Search for Command Prompt, right-click the top result, and select the Run as administrator option.
Type the path and name of the batch file, and press Enter:
C:PATHTOFOLDERBATCH-NAME.bat
For example, the following command runs the batch file located in the 'scripts' folder, inside the 'Downloads' folder:
C:UsersuserDownloadsscriptsfirst_basic_batch.bat
© Provided by Windows Central
After you complete the steps, the console will return the results, and the window won't close even if the script doesn't include the 'PAUSE' command.
File Explorer
To run a batch file with File Explorer, use these steps:
- Open File Explorer.
- Browse to the folder with the script.
- Double-click the batch file to run it.
(Optional) If you execute a command that requires administrator privileges, you'll need to run the script as an admin by right-clicking the batch file and selecting the Run as administrator option.
© Provided by Windows Central- Click the Yes button
Once you complete the steps, the batch will run each command in sequence displaying the results in the terminal.
Run batch file on schedule
To schedule a batch file on Windows 10, you can use the Task Scheduler with these steps:
- Open Start.
- Search for Task Scheduler and click the top result to open the app.
- Right-click the 'Task Scheduler Library' branch and select the New Folder option.
Type a name for the folder — for example, MyScripts.
Quick note: It's not a requirement to create a folder, but it's recommended to keep tasks organized.
- Click the OK button.
- Expand the 'Task Scheduler Library' branch.
- Right-click the MyScripts folder.
Select the Create Basic Task option.
© Provided by Windows CentralIn the 'Name' field, type a descriptive name for the task — for example, SystemInfoBatch.
© Provided by Windows Central- (Optional) In the 'Description' field, create a description for the task.
- Click the Next button.
Select the Monthly option.
© Provided by Windows CentralQuick note: On Windows 10, the Task Scheduler allows you to choose from different triggers, including a specific date, during startup, or when a user signs in to the device. In this case, we're selecting the option to run a task every month, but you may need to configure additional parameters depending on your requirements.
- Click the Next button.
- Using the 'Start' settings, confirm the day and time to start running the task.
Use the 'Monthly' drop-down menu to pick the months of the year you want to run the task.
© Provided by Windows CentralUse the 'Days' or 'On' drop-down menu to confirm the days the task will run.
© Provided by Windows Central- Click the Next button.
Select the Start a program option to run the batch file.
© Provided by Windows Central- In the 'Program/script' field, click the Browse button.
Select the batch file you created.
© Provided by Windows Central- Click the Finish button.
Once you complete the steps, the task will save and run the script on a schedule.
These instructions cover the steps to create a basic task with Task Scheduler. If you want to create a more customizable task, use these instructions.
Run batch files on startup
If you want to execute a sequent of commands every time you sign in to your Windows 10 account, instead of using Task Scheduler, you can place the script in the 'startup' folder to save the extra steps.
To run a script on startup on Windows 10, use these easy steps:
- Open File Explorer.
- Browse to the folder with the batch file.
- Right-click the batch file and select the Copy option.
- Use the Windows key + R keyboard shortcut to open the Run command.
Type the following command:
shell:startup
© Provided by Windows Central- Click the OK button.
Click the Paste option from the 'Home' tab in the Startup folder. (Or Click the Paste shortcut button to create a shortcut to the batch file.)
© Provided by Windows Central- Sign out of your account.
- Sign back into the account.
After you complete the steps, every time you sign in to Windows 10, the batch file will execute and run the included commands.
We're focusing this guide on Windows 10, but the ability to run batch files has been around for a long time, which means that you can refer to these instructions if you are still on an older version, including Windows 8.1 or Windows 7.
More Windows 10 resources
For more helpful articles, coverage, and answers to common questions about Windows 10, visit the following resources:
In this tutorial, you will learn about batch file commands and how they are used in batch file scripting or programming.
As discussed in the previous tutorial, a batch file is an unformatted text file or script file which contains multiple batch file commands or instructions to achieve a certain task. It has extension of .bat
or .cmd
.
Click here to go through the introduction of the batch file before learning batch file commands.
Batch file commands: Windows/DOS
For the ease of learning, we have listed all the batch file commands with relevant examples and explanations below. Please click on the commands to know the details.
ASSOC | ATTRIB | CD | CHKDSK | CHOICE |
CLS | CMD | COMP | CONVERT | COPY |
DATE | DEL | DIR | DISKPART | DRIVERQUERY |
ECHO | EXIT | EXPAND | FC | FIND |
FORMAT | HELP | IPCONFIG | LABEL | MD |
MORE | MOVE | NET | PAUSE | PING |
RD | REM | REN | SET | SHUTDOWN |
SORT | START | SYSTEMINFO | TASKKILL | TASKLIST |
TIME | TITLE | TREE | TYPE | VER |
VOL | XCOPY |
Note: Batch file commands are not case sensitive
ASSOC
The batch command ASSOC
associates a file extension with a file type, or list all associations.
Example
Output
As shown in above output, it displays the file association for .txt
extension.
If only ASSOC
is written and executed, it will display all the file associations for every extension, instead of just .txt
extension.
ATTRIB
The batch command ATTRIB
is used to display the file attributes or set an attribute to a file in the working directory.
Example
Now let us suppose we have a file note.txt
in our working directory. We will display its file attributes and then make it hidden and read only by adding 'ah'
and 'r'
attributes to it. And finally, we will remove some attributes we added as well.
Output
Here in this output, A means Archived, R means Read only and AH means Hidden file.
CD
The batch command CD
helps in navigating through different directories and changing directories or displaying current directory.
Example
Output
CHKDSK
The batch command CHKDSK
is used for checking error in the disk.
Example
CHOICE
The batch command CHOICE
provides a list of options to the user.
Example
Output
Now that script will produce following output.
Now the console waits for your input and once you enter your answer it will terminate.
CLS
The batch command CLS
clears the screen.
Example
This command just clears all the logs in command prompt screen.
CMD
The batch command CMD
invokes a new command prompt window.
Example
COMP
The batch command COMP
compares the size of two files and checks if they are different in size.
Example
CONVERT
The batch command CONVERTS
the volumes or drives from one format to another i.e from FAT to NTFS.
Example
COPY
The batch command COPY
is used for copying files from one location to another.
Example
DATE
The batch command DATE
displays the current date in the system.
Example
Output
This command DATE displays system date in command prompt as shown above.
DEL
The batch command DEL
is used for deleting files.
Example
Note: DEL command only deletes files, not directories.
DIR
The batch command DIR
lists all the contents of directories.
Example
DISKPART
The batch command DISKPART
shows the properties of a disk partition.
Example
This script will ask for users permission to check the properties of disk partition and if allowed, will display following output in the console depending on disk properties.
DRIVERQUERY
The batch command DRIVERQUERY
displays all the drivers installed and their properties.
Example
Output
This output shows the fraction of drivers list with their properties and installed date. DRIVERQUERY
command shows all the drivers list, which is huge.
ECHO
The batch command ECHO
is used for echoing commands on/off and printing message to the console.
Example
Output
This command ECHO displays Hello in the console as shown above.
Besides printing message, echo
is also used for deciding whether or not to display the command itself. Like in this example as well, in the first line we have turned OFF echo, which makes sure that commands themselves are not printed.
If that echo wasn't turned off, then the output would have been like:
EXIT
The batch command EXIT
terminates and exits the console.
Example
In this example, as soon as HI
is printed in the console, EXIT
command will terminate the program and close the output console.
EXPAND
The batch command EXPAND
extracts the contents of .cab
file.
Example
This script will extract all the contents of xyz.cab
file to the same location where xyz.cab
is located.
FC
The batch command FC
finds the difference between the two files and displays them to console.
Example
This script will find the difference in the content of both files and list out all of them.
FIND
The batch command FIND
search the given file to find the desired string and if located, it displays the corresponding line in which the string exists.
Example
This script will search for the string 'find me' in example.txt file and if it exists in example.txt, it will display the corresponding line on the console.
FORMAT
The batch command FORMAT
is used for formatting a drive of format FAT 16/32 or NTFS in Windows.
Example
This script will format E drive and overwrite previous contents.
HELP
This might be the one of the most important batch file commands because with this HELP
command we can know about all the other commands used in batch file or command prompt.
Example
Now this will display all the available commands with their functionalities in the console.
Since the list of commands is so much more, we have sliced the list and shown few here.
Now that we have a list of batch file commands, we can also view details of their syntax and functionalities in following way:
Now this will display details of the copy command.
As you can see, it HELP COPY
displays all the details about COPY
command.
IPCONFIG
The batch command IPCONFIG
displays Windows IP configuration.
Example
This script will generate following output.
P.S: We have hidden iPV6 address in above output.
LABEL
The batch command LABEL
displays the label of a drive or volume and is also is used for adding, setting or removing a disk label.
Example
Now this will display the label of your working directory and you can set, remove or add another label.
For example, my working directory is D: and has label ‘apps'. So, it will generate following output:
MD
The batch command MD
creates a new directory or folder in the working directory.
Example
This program will create a new directory abc in current working location.
MORE
The batch command MORE
displays the content of a file one by one.
Example
This program will display the contents of example.txt line by line, one at a time.
MOVE
This batch command moves files from one directory to another, rename the directories and move the directories as well.
Example
In this way, MOVE
command can be used to move files, directories and rename directories.
NET
The batch command NET
is used for many network functionalities depending upon the commands used.
Example
If that echo wasn't turned off, then the output would have been like:
EXIT
The batch command EXIT
terminates and exits the console.
Example
In this example, as soon as HI
is printed in the console, EXIT
command will terminate the program and close the output console.
EXPAND
The batch command EXPAND
extracts the contents of .cab
file.
Example
This script will extract all the contents of xyz.cab
file to the same location where xyz.cab
is located.
FC
The batch command FC
finds the difference between the two files and displays them to console.
Example
This script will find the difference in the content of both files and list out all of them.
FIND
The batch command FIND
search the given file to find the desired string and if located, it displays the corresponding line in which the string exists.
Example
This script will search for the string 'find me' in example.txt file and if it exists in example.txt, it will display the corresponding line on the console.
FORMAT
The batch command FORMAT
is used for formatting a drive of format FAT 16/32 or NTFS in Windows.
Example
This script will format E drive and overwrite previous contents.
HELP
This might be the one of the most important batch file commands because with this HELP
command we can know about all the other commands used in batch file or command prompt.
Example
Now this will display all the available commands with their functionalities in the console.
Since the list of commands is so much more, we have sliced the list and shown few here.
Now that we have a list of batch file commands, we can also view details of their syntax and functionalities in following way:
Now this will display details of the copy command.
As you can see, it HELP COPY
displays all the details about COPY
command.
IPCONFIG
The batch command IPCONFIG
displays Windows IP configuration.
Example
This script will generate following output.
P.S: We have hidden iPV6 address in above output.
LABEL
The batch command LABEL
displays the label of a drive or volume and is also is used for adding, setting or removing a disk label.
Example
Now this will display the label of your working directory and you can set, remove or add another label.
For example, my working directory is D: and has label ‘apps'. So, it will generate following output:
MD
The batch command MD
creates a new directory or folder in the working directory.
Example
This program will create a new directory abc in current working location.
MORE
The batch command MORE
displays the content of a file one by one.
Example
This program will display the contents of example.txt line by line, one at a time.
MOVE
This batch command moves files from one directory to another, rename the directories and move the directories as well.
Example
In this way, MOVE
command can be used to move files, directories and rename directories.
NET
The batch command NET
is used for many network functionalities depending upon the commands used.
Example
To display the users:
This will generate following output:
Like users, there are many other commands:
- net accounts
- net computer
- net config
- net continue
- net file
- net group
- net help
- net name
- net pause
- net print
- net send
- net session
- net share
- net start
- net statistics
- net stop
- net time
- net use
- net view
PATH
The batch command PATH
displays the path variable or it can be used to set path variable.
Example
This program will display the path of the current working directory.
PAUSE
The batch command PAUSE
is used for holding the output screen till user enters a variable or a value.
Example
This program will print hi in the console and show the message ‘Press any key to continue.' and wait for the input from the user.
PING
The batch command PING
is used for sending ICMP/IP packets to the designated address over the network.
Example
Output
This script will send packets to address 127.0.1.1 and output will be displayed as follows:
RD
The batch command RD
is used for removing the empty directories, directories with contents or files inside cannot be removed with RD
command.
Example
REM
The batch command REM
signifies comments in the batch script.
Example
Anything written after REM
is interpreted as a comment and is not executed in batch programs.
REN
The batch command REN
is used for renaming files and directories.
Example
SET
The batch command SET
displays the list of environment variables of the system.
Example
SHUTDOWN
The batch command SHUTDOWN
when invoked, shuts down the computer.
Example
SORT
The batch command SORT
is used to sort the content of the file alphabetically.
Example
This script will sort the content of example.txt alphabetically either in ascending or descending order.
START
The batch command START
is used to open a file or start a new program.
Example
This program will start the application paint if it is in the working location, else you will have to explicitly indicate the path of that program as well.
SYSTEMINFO
The batch command SYSTEMINFO
displays all the configuration of the computer and operating system.
Example
This will generate following output depending upon the computer:
Of course, the details will be much more than this, but please try and look on your PC.
Batch File Tutorial Pdf
TASKKILL
The batch command TASKKILL
is used to terminate a running task
Example
If you were to terminate the notepad running in your PC, then following script is used.
TASKLIST
The batch command TASKLIST
lists all the running tasks in the console.
Example
TIME
The batch command TIME
is used to display or set the system time.
Example
Output
The current time is displayed in the console.
TITLE
The batch command TITLE
sets new title for output console.
Example
This script will set the title of output console to ‘New Console'. Thus the output console will look like:
TREE
The batch command TREE
displays the tree diagram of the subdirectories to the last level.
Example
I have a folder movies. SO, if I were to run TREE
in that movie folder, it will create a tree of movies inside that folder and also the subdirectories where there is movie file and subtitle file.
Output
This script will generate following output.
TYPE
The batch command TYPE
is used for displaying the content of a file to an output console.
Example
This program will display all the contents of notes.txt
to the console.
VER
The batch command VER
displays the version of Windows or MS-DOS.
Example
Output
VOL
The batch command VOL
displays the current volume label of Windows.
Example
Output
Basic Batch Files
XCOPY
The batch command XCOPY
is similar to COPY
command but COPY
command copies single file whereas XCOPY
command copies entire directories including subdirectories.
Example
This script will copy test.txt from D drive to E drive.
So, these are the batch file commands along with examples.
Basic Batch Files
We hope you find these batch file commands easy to learn. We will discuss more advanced concepts in next tutorials.