You 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 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
The above script outputs the phrase, "Congratulations! Your first batch file executed successfully" on the terminal screen.
- @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
The 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.
- @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.
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-SHARE\FOLDER-NAME /user:YOUR-USERNAME YOUR-PASSWORD
Quick 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.
- 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:\PATH\TO\FOLDER\BATCH-NAME.bat
For example, the following command runs the batch file located in the "scripts" folder, inside the "Downloads" folder:
C:\Users\user\Downloads\scripts\first_basic_batch.bat
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.
- 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.
-
In the "Name" field, type a descriptive name for the task — for example, SystemInfoBatch.
- (Optional) In the "Description" field, create a description for the task.
- Click the Next button.
-
Select the Monthly option.
Quick 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.
-
Use the "Days" or "On" drop-down menu to confirm the days the task will run.
- Click the Next button.
-
Select the Start a program option to run the batch file.
- In the "Program/script" field, click the Browse button.
-
Select the batch file you created.
- 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
- 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.)
- 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:
0 comments:
Post a Comment