How to create a BAT (Batch) file on Windows to perform quick operations

On Windows we are used to using only the buttons and menu items to be able to start programs and to perform actions on the PC; at the dawn of the Microsoft operating system all operations were carried out from the command line or using some simple scripts called File Batch, so that you can obtain a series of commands with a single click. The extension of the script files created was BAT, with which we could combine multiple command lines into a simple text file executable from any version of Windows (compatibility with BAT files is also maintained on Windows 10, the latest version of the system Microsoft operating).
Going back from the past, in this guide we will show you how to speed up some common operations on Windows without having to use the graphical interface every time, showing you how to create a BAT file (or Batch file ) and how to start it on the operating system. In addition to starting the script immediately, we will also show you how to time it, so that you can start it at a specific time or after a certain period of time.
READ ALSO: Windows Notepad tricks

How to create a BAT file on Windows

Below we will discover everything you need to be able to create a Batch file with the .BAT extension on any version of Windows still supported by Microsoft (from Windows 7 up to Windows 10). On paper, BAT files are also compatible with older versions of Windows, but we do not recommend using them on them since, being now out of support, they can present major security problems.

How to create a BAT file

To create a BAT file on Windows simply open the Start menu in the lower left and look for Notepad .

It is our tool with which we can create any type of BAT file, appropriately adjusting the file extension (as we will see below). If we want to use a more advanced Notepad program to make BAT files on Windows, we recommend installing the Notepad ++ advanced notepad, open source and available for free.
Once the favorite notebook is opened, we insert one or more command lines into the text field, so we can start compiling the script. To maintain order and to understand if the script has been executed, we recommend that you write on top of it with the following command:
@echo off
this will allow us to view the result of the execution, without necessarily showing the commands made. Once the necessary command lines have been inserted, we add to the script with the command:
breaks
If we want the window to wait for our confirmation for closing or, alternatively, we can close the script with the command:
cls
That will immediately close the window as soon as the script is executed. Once the command has been compiled, click on the File menu at the top left, select Save As, choose which folder to save the script in, open the drop-down menu next to the Save as item, set All files (*. *) And finally we choose a name for the script in the File name field, taking care to also add the .bat extension.

To create the BAT file, simply click on the Save button below. The script is ready, all we have to do is double-click on it to start executing it. In the following paragraph we will show you some ready-made script commands that we can insert between @echo off and pause, so as to have some simple scripts immediately available.

Operations to be performed on a BAT file


Inside the BAT file we can insert any command supported by the Windows prompt, but for convenience we have collected what can be some simple commands to insert inside a BAT file, even just for testing:
@echo off
shutdown –s –f –t 0
breaks
With this script we can start the PC shutdown procedure, without going through the Start menu; next to the main shutdown command there are arguments that enhance its effectiveness: -s specifies that we must shutdown the PC, -f forces the shutdown of all active programs and -t allows you to set a period of time before of the execution (with 0 the execution will be immediate, but we can also choose 60, 90 etc. as needed; the time is calculated in seconds).
@echo off
shutdown –r –f –t 0
breaks
Practically identical to the previous command, with the difference that the latter will restart the PC instead of turning it off.
@echo off
shutdown –h –f –t 0
breaks
The command is similar to the previous ones, only it will start hibernating the computer instead of shutting it down completely.
@echo off
rd /sc:\$Recycle.Bin
breaks
With the previous command we will be able to empty the recycle bin by starting the script and confirming with S and Enter on the keyboard to the cancellation request. In order to run the BAT file correctly, we right-click on it and use the Run as administrator item .
@echo off
wuauclt.exe / detectnow / updatenow
breaks
With this command we will be able to search for any Windows updates, so that we can download and install them on our computer. In order to run the BAT file correctly, we right-click on it and use the Run as administrator item .
@echo off
cd "C: \ Program Files \ Windows Defender"
MpCmdRun.exe -SignatureUpdate -MMPC
MpCmdRun.exe -Scan -ScanType 1
breaks
With these commands we will update the definitions of Windows Defender (the antivirus integrated in Windows 10) and then we will start the quick scan of the whole PC. In order to run the BAT file correctly, we right-click on it and use the Run as administrator item .
To create your own scripts we can use MS-DOS commands or command prompt; in the latter case, simply open the Start menu at the bottom left, search for the Command Prompt program, start it and type help, so as to display all the most common commands that we can use to create our BAT script.

How to schedule the execution of a BAT file


In addition to using the -t argument to be able to schedule the execution of a specific command, we can schedule the entire BAT file using the Scheduler program, which we can search for within the Windows Start menu.

Once the program window is open, we click on the right side on Create activity, choose in the first screen a name to assign to the activity then go to the Actions tab, so you can click on the New button.

In the new window, make sure that the Start program item is set in the Operation field, then click on the Browse button and select the BAT script that we previously created. Once the file has been added, click OK on the bottom, go to the Activation tab, press New and choose the time conditions with which Windows must automatically perform the activity.

We carefully choose the activity in the Start activity field, then we choose whether to run the BAT script at regular intervals, when the PC starts up or when the user logs on. At the end of the configuration we click OK, we check the other tabs to check if the activation conditions reflect our needs then we create the new scheduled activity by clicking OK .
Note : if we have created a BAT script that requires administrator permissions to be able to run correctly, make sure to tick the box next to the item Run with the highest privileges in the General tab.

Conclusions


With Batch files we can speed up the execution of some common operations such as shutting down, restarting and hibernating the computer and some simple maintenance procedures such as emptying the recycle bin and launching a scan of the antivirus integrated in Windows 10. Obviously we can create an infinity of different BATs according to our needs and our command prompt management capabilities.
If we are looking for other advanced tricks on how to make the best use of the Windows command prompt, please read our Guide to Tricks for Command Prompt (CMD) . Always from a prompt we can use some very useful control and analysis tools to manage the computer, as described in our article on the 5 useful tools hidden in the Windows command line .

Leave Your Comment

Please enter your comment!
Please enter your name here