Friday, 16 October 2020

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:

  1. Open Start.
  2. Search for Notepad and click the top result to open the app.
  3. 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.
  4. Click the File menu.
  5. Select the Save as option.
  6. 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:

  1. Open Start.
  2. Search for Notepad and click the top result to open the app.
  3. 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.
  4. Click the File menu.
  5. Select the Save as option.
  6. 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:

  1. Open Start.
  2. Search for Notepad and click the top result to open the app.
  3. 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.

  4. Click the File menu.
  5. Select the Save as option.
  6. 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.

  1. Open Start.
  2. Search for Command Prompt, right-click the top result, and select the Run as administrator option.
  3. 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:

  1. Open File Explorer.
  2. Browse to the folder with the script.
  3. Double-click the batch file to run it.
  4. (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.

  5. 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:

  1. Open Start.
  2. Search for Task Scheduler and click the top result to open the app.
  3. Right-click the "Task Scheduler Library" branch and select the New Folder option.
  4. 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.

  5. Click the OK button.
  6. Expand the "Task Scheduler Library" branch.
  7. Right-click the MyScripts folder.
  8. Select the Create Basic Task option.

  9. In the "Name" field, type a descriptive name for the task — for example, SystemInfoBatch.

  10. (Optional) In the "Description" field, create a description for the task.
  11. Click the Next button.
  12. 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.

  13. Click the Next button.
  14. Using the "Start" settings, confirm the day and time to start running the task.
  15. Use the "Monthly" drop-down menu to pick the months of the year you want to run the task.

  16. Use the "Days" or "On" drop-down menu to confirm the days the task will run.

  17. Click the Next button.
  18. Select the Start a program option to run the batch file.

  19. In the "Program/script" field, click the Browse button.
  20. Select the batch file you created.

  21. 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:

  1. Open File Explorer.
  2. Browse to the folder with the batch file.
  3. Right-click the batch file and select the Copy option.
  4. Use the Windows key + R keyboard shortcut to open the Run command.
  5. Type the following command:

    shell:startup

  6. Click the OK button.
  7. 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.)

  8. Sign out of your account.
  9. 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

ShortNewsWeb

Blog Archive

Categories

'The Woks of Life' Reminded Me to Cook With All the Flavors I Love (1) 10 Scary Podcasts to Listen to in the Dark (1) 13 of the Best Spooky Episodes From (Mostly) Un-Spooky Shows (1) 13 Spooky Movies Set on Halloween Night (1) 16 of the Best Ways to Declutter Your Home (1) 1Password Now Generates QR Codes to Share Wifi Passwords (1) 2024 (15) 21 Thanksgiving Movies About Families As Screwed-Up As Yours (1) 30 Movies and TV Shows That Are Basically 'Competence Porn' (1) 30 of the Most Obscenely Patriotic Movies Ever (1) 31 Spooky Movies to Watch Throughout October (1) 40 Netflix Original Series You Should Watch (1) 55 Box Office Bombs Totally Worth Watching (1) Active Directory (1) Adobe's AI Video Generator Might Be as Good as OpenAI's (1) AIX (1) and I'd Do It Again (1) and Max Bundle Isn't a Terrible Deal (1) and the Dreo Solaris Is the Best Space Heater I’ve Tried (1) and These Are My Favorite Tech Deals From Walmart’s Black Friday Sale (1) and These Water-Resistant Running Shoes Are a Game Changer (1) and They're All on Sale for Black Friday (1) Apache (2) Apple Intelligence Is Running Late (1) Apple Intelligence's Instructions Reveal How Apple Is Directing Its New AI (1) Apple Passwords Is Now on Firefox (but Not for Windows Users) (1) August 18 (1) August 4 (1) August 5 (1) Avoid an Allergic Reaction by Testing Your Halloween Makeup Now (1) Backup & Restore (2) best practices (1) bleepingcomputer (76) Blink Security Cameras Are up to 68% Off Ahead of Prime Day (1) Bluesky Has Trending Topics Now (But You Can Disable Them) (1) CentOS (1) Configure PowerPath on Solaris (1) Documents (2) Don't Fall for This 'New' Google AI Scam (1) Don't Rely on a 'Monte Carlo' Retirement Analysis (1) Eight Cleaning Products TikTok Absolutely Loves (1) Eight of the Best Methods for Studying so You Actually Retain the Information (1) Eight Unexpected Ways a Restaurant Can Mislead You (1) Elevate Your Boring Store-Bought Pretzels With This Simple Seasoning Technique (1) Everything Announced at Apple's iPhone 16 Event (1) file system (6) Find (1) Find a Nearby ‘Gleaning Market’ to Save Money on Groceries (1) Five Red Flags to Look for in Any Restaurant (1) Five Ways You Can Lose Your Social Security Benefits (1) Flappy Bird's Creator Has Nothing to Do With Its 'Remake' (1) Four Reasons to Walk Out of a Job Interview (1) Four Signs Thieves Are Casing Your House (1) gaming (1) Goldfish Crackers Have a New Name (for a Little While) (1) Hackers Now Have Access to 10 Billion Stolen Passwords (1) How I Finally Organized My Closet With a Digital Inventory System (1) How I Pack Up a Hotel Room So I Don’t Forget Anything (1) How to Buy Residency in Another Country With a 'Golden Visa' (1) How to Cancel Your Amazon Prime Membership After Prime Day Is Over (1) How to Choose the Best Weightlifting Straps for Your Workout (1) How to Enable (and Turn Off) Apple Intelligence on an iPhone (1) How to Get Started With Bluesky (1) How to Keep Squirrels Off Your Bird Feeders (1) How to Remotely Control Another iPhone or Mac Using FaceTime (1) How to Set Up Your Bedroom Like a Hotel Room (and Why You Should) (1) How to Speak With a Real Person at Target Customer Service (1) How to Take a Screenshot on a Mac (1) How to Take Full Control of Your Notifications on a Chromebook (1) How to Use Picture-in-Picture Mode on an Android Phone (1) Hulu (1) I Chose the Beats Fit Pro Over the AirPods Pro (1) I'd Recommend These Seven Outdoor Security Cameras I've Tested (1) I'm a Runner (1) I'm a Shopping Writer (1) I’m Always Cold (1) If You Got a Package You Didn't Order (1) If You Hate Running (1) Important Questions (17) Install and Configure PowerPath (1) interview questions for linux (2) Is ‘Ultra-Processed’ Food Really That Bad for You? (1) Is Amazon Prime Really Worth It? (1) It Might Be a Scam (1) July 14 (1) July 21 (1) July 28 (1) July 7 (1) June 30 (1) LifeHacker (158) Linux (36) Make and Freeze Some Roux Now for Easy Turkey Gravy (1) Meredith's Training Diaries: How I Crushed My Marathon Personal Record (1) Meta Releases Largest Open-Source AI Model Yet (1) Monitoring (3) music (688) My Favorite 14TB Hard Drive Is 25% Off Right Now (1) My Favorite Amazon Deal of the Day: Apple AirPods Max (2) My Favorite Amazon Deal of the Day: Apple Pencil Pro (1) My Favorite Amazon Deal of the Day: Google Nest Mesh WiFi Router (1) My Favorite Amazon Deal of the Day: Google Pixel 8 (1) My Favorite Amazon Deal of the Day: PlayStation 5 (1) My Favorite Amazon Deal of the Day: Samsung Odyssey G9 Gaming Monitor (1) My Favorite Amazon Deal of the Day: SHOKZ OpenMove Bone Conduction Headphones (1) My Favorite Amazon Deal of the Day: The 13-Inch M3 Apple MacBook Air (1) My Favorite Amazon Deal of the Day: The Samsung Galaxy Buds 3 Pro (1) My Favorite Amazon Deal of the Day: The Sonos Era 100 (1) My Favorite Amazon Deal of the Day: These Bose QuietComfort Headphones (1) My Favorite Tools for Managing Cords and Cables (1) Nagios (2) Newtorking (1) NFS (1) OMG! Ubuntu! (688) Oracle Linux (1) oracleasm (3) osnews (31) Password less communication (1) Patching (2) Pixel Studio Is the Easiest (If Not the Best) Way to Make AI Art on Your Pixel 9 (1) Poaching Is the Secret to Perfect Corn on the Cob (1) powerpath (1) Prioritize Your To-Do List By Imagining Rocks in a Jar (1) Red Hat Exam (1) register (89) Rsync (1) Safari’s ‘Distraction Control’ Will Help You Banish (Some) Pop Ups (1) Samba (1) Save Time and Air Fry Your Pumpkin Pie (1) Scrcpy (1) September 1 (1) September 15 (1) September 2 (1) September 22 (1) September 23 (1) September 30 (1) September 8 (1) Seven Home 'Upgrades' That Aren’t Worth the Money (1) Seven Things Your Credit Card’s Trip Protection Won’t Actually Cover (1) Six Unexpected Household Uses for Dry-Erase Markers (1) ssh (1) Swift Shift Is the Window Management Tool Apple Should Have Built (1) System hardening (1) Tailor Your iPhone's Fitness Summary to Your Workouts (1) Target’s ‘Circle Week’ Sale Is Still Going After October Prime Day (1) Target’s Answer to Prime Day Starts July 7 (1) Tech (9560) Tech CENTRAL (35) Technical stories (145) technpina (10) The 30 Best Movies of the 2020s so Far (and Where to Watch Them) (1) The 30 Best Sports Movies You Can Stream Right Now (1) The Best Deals on Robot Vacuums for Amazon’s Early Prime Day Sale (2) The Best Deals on Ryobi Tools During Home Depot's Labor Day Sale (1) The Best Early Prime Day Sales on Power Tools (1) The Best Movies and TV Shows to Watch on Netflix This Month (1) The Best October Prime Day Deals If You Are Experiencing Overwhelming Existential Dread (1) The Best Places to Go When You Don't Want to Be Around Kids (1) The Best Places to Order Thanksgiving Dinner to Go (1) The Best Strategies for Lowering Your Credit Card Interest Rate (1) The Best Ways to Store All Your Bags and Purses (1) The Latest watchOS Beta Is Breaking Apple Watches (1) The Marshall Emberton II Speakers Are $70 Off for Black Friday (1) The New Disney+ (1) The Samsung Galaxy Buds 3 Pro Are $60 Off for Black Friday (1) The Two Best Times of Year to Look for a New Job (1) the X Rival Everyone's Flocking To (1) These Bissell Vacuums Are on Sale Ahead of Black Friday (and They're All Great) (1) These Meatball Shots Are My Favorite Football Season Snack (1) These Milwaukee Tools Are up to 69% off Right Now (1) This 2024 Sony Bravia Mini-LED TV Is $400 Off Right Now (1) This 75-Inch Hisense ULED 4K TV Is $500 Off Right Now (1) This Google Nest Pro Is 30% Off for Prime Day (1) This iPhone and Mac App Lets You Edit Your Bluesky Posts (1) This MagSafe-Compatible Power Bank Is 40% Off for Black Friday (1) This Peanut Butter Latte Isn’t As Weird As It Sounds (1) This Tech Brand Will Get the Biggest Discounts During Prime Day (1) Three Quick Ways to Shorten a Necklace (1) Three Services People Don't Know They Can Get From Their Bank for Free (1) Today’s Wordle Hints (and Answer) for Monday (4) Today’s Wordle Hints (and Answer) for Sunday (11) Try 'Pile Cleaning' When Your Mess Is Overwhelming (1) Try 'Pomodoro 2.0' to Focus on Deep Work (1) Try 'Rucking' (1) Ubuntu News (349) Ubuntu! (1) Unix (1) Use This App to Sync Apple Reminders With Your iPhone Calendar (1) Use This Extension to Find All Your X Followers on Bluesky (1) veritas (2) Videos (1) Was ChatGPT Really Starting Conversations With Users? (1) Watch Out for These Red Flags in a Realtor Contract (1) Wayfair Is Having a '72-Hour Closeout' Sale to Compete With Prime Day (1) We Now Know When Google Will Roll Out Android 15 (1) What Is the 'Die With Zero' Movement (and Is It Right for You)? (1) What Not to Do When Training for a Marathon (1) What to Do When Your Employer Shifts Your Pay From Salary to Hourly (1) What to Look for (and Avoid) When Selecting a Pumpkin (1) What to Wear to Run in the Cold (1) What's New on Max in December 2024 (1) What's New on Prime Video and Freevee in September 2024 (1) Why You Can't Subscribe to Disney+ and Hulu Through Apple Anymore (1) Why Your Home Gym Needs Adjustable Kettlebells (1) Windows (5) You Can Easily Add Words to Your Mac's Dictionary (1) You Can Get 'World War Z' on Sale for $19 Right Now (1) You Can Get a Membership to BJ's for Practically Free Right Now (1) You Can Get Beats Studio Buds+ on Sale for $100 Right Now (1) You Can Get Microsoft Visio 2021 Pro on Sale for $20 Right Now (1) You Can Get This 12-Port USB-C Hub on Sale for $90 Right Now (1) You Can Get This Roomba E5 Robot Vacuum on Sale for $170 Right Now (1) You Can Hire Your Own Personal HR Department (1) You Can Search Through Your ChatGPT Conversation History Now (1) You Can Set Different Scrolling Directions for Your Mac’s Mouse and Trackpad (1) Your Verizon Bill Just Got a Little More Expensive (1)

Recent Comments

Popular Posts

Translate

My Blog List

Popular

System Admin Share

Total Pageviews