How to Create and Run Batch (BAT) File On Windows?

A batch file is a file that contains MS-DOS commands and when the batch files are clicked or called these commands are executed. Batch files also called BAT files. Most of the operating systems like windows provide the ability to create and run batch files. In this tutorial, we will learn how to create a batch file for MS-DOS and PowerShell, How To Run, or Execute Batch File?

Batch files are supported by all modern Windows operating system versions like Windows XP, Windows 7, Windows 8, Windows 10 and Windows Server versions. But the batch files contents like commands, binaries and features should be compatible with the current operating system in order to execute in reliable way. If specific command, binary or feature is not supported it will be skipped and not executed but others lines of batch file will be executed.

What Is Batch File?

Before starting to create and running batch files we should learn what is a batch file. A batch file is a simple text file that has the *.bat file extension and contains MS-DOS commands or binary files. Batch files also balled as BAT files because of their extension. Batch files are popular among system administrators and technical people in order to complete repetitive or non-interactive jobs easily and reliably. Even interactive jobs can be completed with batch files with little or no effort or action.

Advantages of Batch/BAT Files

Batch or BAT files are a lot of advantages for Windows users. Below we will list some of them.

  • Offload repetitive tasks and automate them.
  • Reliable command execution
  • Noninteractive command execution.
  • Advanced reporting and performance tracking
  • Scheduled tasks can be executed on different dates and times.
READ  Windows cmd.exe (Command Interpreter) Tutorial

Create Batch File

Batch files are a simple text file which contains MS-DOS and related commands and binaries. First, open Notepad and create a file and save the file whatever name you want but set the file extension as *.bat .In this example we will create the batch file named runwise.bat . Batch files may also use extensions like .cmd and .btm.

mkdir Test
@ECHO OFF
Welcome a folder named Test has been created
PAUSE
Save Batch File

The saved batch file will look like below. As you can see the batch files or files with *.bat extension has the following file icon which simply used for system-related files. By default, the .bat extension is not displayed.

Batch File on Desktop

Run Batch File

In order to run batch files content and commands batch files should be executed properly. Batch files can be executed in the following ways.

  • Clicking on Batch File
  • Using Scheduled Jobs
  • Calling From MS-DOS or PowerShell Command Lines
  • Calling Another Batch File

Run Batch File By Clicking

The simplest and most popular method to run a batch file is just clicking on the batch file. This is a GUI method to run a batch file where it can be used from desktop or file explorer. Double-clicking on a batch file will run the batch file in an MS-DOS command prompt.

Run Batch File By Clicking

Run Batch File Using Scheduled Jobs

Scheduled Tasks is a tool used to schedule different jobs or tasks to run the future at the specified date or time or repetitively etc. It is recently called as Task Scheduler and can be opened by typing schedule in the start menu like below.

Open Task Schedular

In the following screen you can create a new task for the batch file. We will click to the Create Basic Task in order to create a new task.

Create New Task

In the following screen we will set the name of the task which is not the batch file name. Then we will click to the Next .

Set Task Name

In the following screen the date or time of the execution will be specified. The batch file will be executed at the specified date, time or interval.

Set Period of Task

As we have selected daily the start date with the time and recur count will be provided in the following screen.

Set Time For Task

This step is important where we will select the Start a program in order to specify our batch file to execute.

Set Task Action

This step is the most important to schedule our batch file. In the program script part, we will specify the script file location. Also, the Browser button can be used to select and specify a batch file location. Also, we can specify arguments to be passed into the batch script from the Add arguments textbox.

Specify The Batch File Path

Following screen is the last screen where we all information about scheduled batch job will be displayed.

Review Scheduled Batch File Configuration

Run Batch File From MS-DOS or PowerShell Command Lines

Batch files can be run from MS-DOS or PowerShell command lines. MS-DOS command line can only run MS-DOS based batch files and con not run PowerShell batch files. But PowerShell command line can run both MS-DOS and PowerShell batch files. In order to run a batch file from the MS-DOS command line just specify the batch file name with the full path. Alternatively, if the batch file is located in the current working directory only the name of the batch file can be called.

C:\Users\ismail\Desktop>runwise.bat
Run Batch File As Command

Or alternatively the complete path of the batch file runwise.bat can be provided from any directory like below.

C:\> C:\Users\ismail\Desktop\runwise.bat
Specify Batch File Complete Path

Run Batch File From Another Batch File

Batch files can call another batch file like a regular MS-DOS command. The most important thing about running a batch file in a batch file is to provide the complete path of the batch file. Even just the name or relative path can be used to run a batch file in a batch file the most reliable way is using the absolute or complete path of the batch file.

mkdir Test
@ECHO ON
Welcome a folder named Test has been created

C:\Users\ismail\Desktop\runwise.bat

Run Batch File As Administrator

If the batch file has commands that need to be executed as Administrator or with Administrative privileges you should open the MS-DOS or PowerShell as Administrator. Please follow the following tutorial in order to open MS-DOS, Command Prompt, or PowerShell as administrator and then use previously described steps like “Run Batch File From MS-DOS or PowerShell command line“.

READ  What Is The Latest Version Of Windows 10 As Today?

Leave a Comment