How to Use Windows Batch File Commands to Automate Repetitive Tasks

Gavin Phillips 01-02-2016

Before Windows became our favorite GUI, everything was done using commands. Some of our readers may remember using MS-DOS commands to complete the smallest of tasks. These days, you can still use commands to automate tasks and speed up your productivity.


If you have a number of boring and repetitive tasks, a batch file is exactly what you’re looking for. Instead of manually executing tasks, a single batch file How to Create a Batch (BAT) File in Five Simple Steps This article covers how to create a batch file using five simple steps for the Windows operating system. Read More can be written to take care of everything.

The Basics: Creating a Batch File

Batch files are written in plain text. You can use whichever plain text editor you like, but the standardized Notepad does the job just fine. If you’re creating really complex programs, it can be handy to have the additional features of Notepad++, but for our examples, we will stick with what we know.

Each example batch file provided has been tested using Notepad. Once we’ve finished inputting our commands, we’ll head to Save As, and give the file an appropriate name. Once saved, we can change the file extension from .txt to .bat, and press Enter. You should notice the file icon immediately change to an application window featuring a cog.

Tasks You Can Automate

Here are a few really useful batch commands for you to play around with, and some short descriptions of what each command syntax and parameter can do 15 Windows Command Prompt (CMD) Commands You Must Know The command prompt is still a powerful Windows tool. Here are the most useful CMD commands every Windows user needs to know. Read More .

1. Open Multiple Programs

This one will let you start multiple programs with a single click. First, identify the programs or files you want to open simultaneously. In this instance, I’ll be opening Chrome, a Word file Yes, Get Microsoft Word for Free: Here's How Don't want to pay for the full Microsoft Word experience? Here are some ways on how you can use Microsoft Word for free. Read More I’m working on, and VMware Player.


Open a new text file and type:

@echo off
cd "C:\Program Files\Google\Chrome\Application\"
start chrome.exe
start – "C:\Program Files\Microsoft Office\Office15\WINWORD.EXE"
"C:\Work\MUO\How to Batch Rename.docx"
cd "C:\Program Files (x86)\VMware\VMware Player"
start vmplayer.exe

Of course, you can add numerous applications and files to this list. For super-opening powers, you can pair this batch file with a hotkey.

Windows Start Multiple Files Batch

The commands we have used here are:

  • @echo displays the command currently being executed in a command shell. We turned this off.
  • cd changes the directory
  • start does the obvious, and starts the program

2. Delete Files Older Than

We can also use a batch file to delete files older than a user-set amount of days. This can be used to delete one specific file type, or a group of files in a folder, so long as they meet the specifications in the batch file. Our first example will delete files in the specified folder older than three days:

forfiles /p "C:\some\file\name\here" /s /m * /d -3 /c "cmd /c del @path"

Our second example will delete only files with the extension .docx, again older than three days:

forfiles /p "C:\some\file\name\here" /s /m * .docx /d -3 /c "cmd /c del @path"

The commands and switches we have used here are:

  • forfiles allows us to use commands for each file in a location i.e. the commands will apply to each file fitting the command arguments
  • /p details the path to start searching i.e. the directory you want to delete the files from
  • /s instructs the command to search sub-directories
  • /m instructs the command to use the given search mask. We used the wildcard operator “*” in our first example, and specified .docx in the second
  • /d-3 is the time setting. Increase or decrease depending on your requirements
  • /c del @path is the delete aspect of the command

3. Backing Up Your System

We can use batch files to back up specific folders, or as part of a wider backup operation How to Factory Reset Windows 10 or Use System Restore Learn how System Restore and Factory Reset can help you survive any Windows 10 disasters and recover your system. Read More . Most people have backups and system restore points setup as standard, but sometimes it pays to make a couple of copies of anything that might make you cry if it were deleted. I’m going to show you one very simple method and another slightly more advanced version.


Windows Batch Backup Test

Method #1

Open Notepad. Type the following, following the instructions:

@echo off
ROBOCOPY C:\your\filename\goes\here C:\your\backup\location\goes\here /LOG:backuplog.txt

Save the file, then rename to systembackup.bat, and press Enter.


This method works well when backing up individual folders, but isn’t entirely practical for anything more complex. The commands used here are:

  • ROBOCOPY stands for Robust File Copy, which replaced when Windows Vista was released

Method #2

This time we will build a longer string of folders we want to back up, including your Outlook address book, and your system registry.

@echo off
:: variables
set drive=X:\Backup
set backupcmd=xcopy /s /c /d /e /h /i /r /y
echo ### Backing up My Documents...
%backupcmd% "%USERPROFILE%\My Documents" "%drive%\My Documents"
echo ### Backing up Favorites...
%backupcmd% "%USERPROFILE%\Favorites" "%drive%\Favorites"
echo ### Backing up email and address book (Outlook Express)...
%backupcmd% "%USERPROFILE%\Application Data\Microsoft\Address Book" "%drive%\Address Book"
%backupcmd% "%USERPROFILE%\Local Settings\Application Data\Identities" "%drive%\Outlook Express"
echo ### Backing up email and contacts (MS Outlook)...
%backupcmd% "%USERPROFILE%\Local Settings\Application Data\Microsoft\Outlook" "%drive%\Outlook"
echo ### Backing up the Registry...
if not exist "%drive%\Registry" mkdir "%drive%\Registry"
if exist "%drive%\Registry\regbackup.reg" del "%drive%\Registry\regbackup.reg"
regedit /e "%drive%\Registry\regbackup.reg"
echo Backup Complete!

A little explanation to what’s going on here: First, we set the location we want the files to be copied to – “set drive=X:\Backup” – followed by the backup command we are going to use – xcopy. Following the xcopy command is a string of parameters telling the batch file to perform a number of tasks:

  • /s copies system files
  • /c carries out the command specified by the string, then terminates
  • /d enables drive and directory changes
  • /e copies empty directories
  • /h copies hidden files
  • /i if destination doesn’t exist, and you’re copying more than one file, /i assumes the destination must be a directory
  • /r overwrites read-only files
  • /y suppresses prompts confirming you want to overwrite read only files

To add more folders to the batch file, use the following syntax:

%backupcmd% "...source directory..." "%drive%\...destination dir..."

We’ve selected a number of folders to copy, but you might note they are all part of your USERPROFILE. If you just want to backup the entire folder, you can use this command, assuming the same “set drive” and “set backupcmd” are in use:

%backupcmd% "%USERPROFILE%" "%drive%\%UserName% - profile"

4. Change Your IP Address

Most of the time your laptop will use a dynamic IP What's an IP Conflict and How Do You Resolve It? What is an IP address conflict and how do they happen? Let's examine what causes IP conflict issues and how to resolve them. Read More address to connect to the Internet. Sometimes, you might be required to use a static IP What Is a Static IP Address? Here's Why You Don't Need One A static IP address is one that never changes. Dynamic IP addresses do change. We explain why you don't need a static IP address. Read More , for instance at your workplace, school, or other “official” locations. Sure, you could manually change it, but if it’s somewhere you visit regularly, why not make a batch file to do the work for you?

For this we’ll create two files: one to make it static, and another to change it back to dynamic.


You should type the following for your static batch file:

netsh interface ip set address "LAN" static "" "" ""

Where the first series of “x’s” is your required static IP, the second is the network/subnet mask, and the third is your default gateway.


This batch file will return your network adaptor settings to a dynamic setup:

netsh int ip set address name = "LAN" source = dhcp

While neither of these files are doing anything by batch, just having those files can be a timesaver. If you have more than one network to connect to, copy the first file and alter the details.

5. Make Your Kids Go to Bed

My kids aren’t old enough to be playing video games in the middle of the night, but I remember my own tactics against my parents so I could play Championship Manager 2 into the small hours of the morning. Luckily, my parents didn’t know about using commands to control my actions. Try this:

@echo off
If %time%==23:30:00.00 goto :X
shutdown.exe /s /f/ t/ 120 /c "GO TO BED RIGHT NOW!!!"

Here, the computer constantly checks to see if the time is half-past eleven. When the time correlates, the message “GO TO BED RIGHT NOW!!!” will display, along with the 120s countdown timer. The 120s should be enough time to save whatever game they are playing, or their work, before the computer shuts down.

To stop this countdown, press Win+R.

Of course, don’t tell the children this.

6. Batch Rename & Mass Delete

I’ve written a more extensive article dealing with batch file renaming and deletion How to Batch Rename & Mass Delete Files in Windows Are you pulling your hair out over how to batch rename or delete hundreds or thousands of files? Chances are, someone else is already bald and figured it out. We show you all the tricks. Read More , so I won’t explore this one too much, but batch files can be used to automate these sometimes tedious tasks. Check out the article for some extended batch commands, and get bulk deleting straight away.

Bonus: Pokémon!

So this has nothing to do with productivity, and if you’re susceptible to Pokémon-related gaming addictions 6 Fun Pokémon Challenges to Prove Your Mastery Do you love Pokémon but feel bored with the series? These Pokémon challenges will breathe new life into your favorite games. Read More , maybe don’t download and play this. If not, you can grab this text-based adventure right here.

Pokemon Batch File

Whetted Your Appetite?

These are just six batch files you can create to automate tasks on your system. With more practice, you’ll be able to accomplish unheralded amounts of activities on your system between batch files and the Command Prompt. And if that doesn’t suit you, you could always take a look at the Windows PowerShell Command Prompt vs. Windows PowerShell: What's the Difference? Windows users can get by without using either the Command Prompt or PowerShell. But with Windows 10 and new features around the corner, maybe it's about time we learned. Read More and AutoHotKey for task automation on Windows The Quick AutoHotkey Guide for Beginners AutoHotkey is a powerful free and open source Microsoft Windows tool that can automate repetitive tasks. Learn how to use it here. Read More .

Related topics: Batch File, Computer Automation.

Affiliate Disclosure: By buying the products we recommend, you help keep the site alive. Read more.

Whatsapp Pinterest

Leave a Reply

Your email address will not be published. Required fields are marked *

  1. Spike Meyers
    October 3, 2019 at 12:43 am

    I wrote this little batch file to backup some data on a daily basis. It executes find from a command prompt, however, when executed from Windows, it terminates from the pause command. The backup does not happen. I can't figure out what I did wrong. Any help would be so very much appreciated.


    ECHO *****************************************************
    ECHO *****************************************************



  2. seth
    July 10, 2017 at 6:48 pm

    love this site.before i only thought that i could make games but notepad has so much potential

  3. shaman
    February 7, 2017 at 6:39 am

    how to put 24 hrs sleep for waiting to another task

  4. Amit
    September 14, 2016 at 11:42 am

    Can you help me with the codes which can be used to restart the system tomorrow on specific time ?

    Also when it should close all the running apps and shut down.

    I dont know codes at all and this will help me alot.

  5. Mike
    September 12, 2016 at 1:23 am

    I've saved hundreds, probably thousands, of man hours writing programs that write batch files.

  6. d o
    September 11, 2016 at 4:39 pm

    At my last job they had a girl creating folders manually every week for dates. I created a batch script that let you type in the year and it created the whole years worth of folders for them for free. They gave me a warning and let me go at the end of my contract. I also got a warning for asking for a second copy of Acrobat so we could do more work, but they eventually purchased another copy of Acrobat, 5 months after I got let go.

  7. Alpha
    September 9, 2016 at 3:01 am

    Is there any tools, batch or script that would allow me to point the location where all a users PST files are kept and will map those pst files at one time rather than mapping each and every pst file one at a time.

    (Example) If the users has multiple PST files mapped in Outlook where they store emails
    based on certain criteria, many of the users have 10, 20 and even 50 individual PST
    files mapped in Outlook. All of the users PST files are store in computer local drive.

    Does anyone know is there such a freeware utility, or is there any batch and script file to be able to execute the task?

  8. Anonymous
    August 9, 2016 at 12:32 pm


  9. Anonymous
    August 9, 2016 at 12:16 pm


  10. Sid
    July 25, 2016 at 8:43 pm

    I have one annoying site that needs me to enter my password everytime it opens. How can i automate my password by using this.

  11. Kannon Yamada
    May 15, 2016 at 10:12 pm

    I regret not reading this article earlier. This is really great stuff. Thanks Gavin!

  12. bromberg
    February 4, 2016 at 5:30 am

    I thought PowerShell was meant for the corporate server environment. If not, how about an article on PowerShell uses for the PC home user?

    • Gavin Phillips
      February 15, 2016 at 2:41 pm

      No, home users can make use of the PowerShell for all sorts of tasks. There is an upcoming article detailing some of these, so be sure to keep an eye out for it. Thanks for reading!

      • dan
        February 16, 2016 at 4:22 am

        I look forward to my daily MUO newsletters and using PowerShell for home PC tasks will get me into some fun and useful projects. Can't wait!

  13. Joe O'Loughlin
    February 2, 2016 at 12:09 pm

    Please tell me how to print this article. I've tried several different tactics but none have been successful. I also tried to email the link to the article to myself but the resulting item couldn't be printed in any readable form. I've tried several of the available screen capture items but have not found one that does the job.

    • Fik of borg
      February 2, 2016 at 1:00 pm

      There are several plugins and extensions to turn web pages printables, but the most inmediate way to do it would be to do it manualliy: Select the text, copy, paste in notepad to wipe the formatting, copy from notepad and paste in your word processor to format the text according to your needs before printing (to PDF, don't waste paper or ink!).
      Of course, after all that you would have the text memorized ...

    • Anonymous
      February 2, 2016 at 5:52 pm

      Have you tried Evernote Clearly? There should be an extension for any browser you're using. Let it do its formatting magic, then you can right click, print or copy, paste into a word processor, depending on your browser.

      Oh...I read that Clearly is going away, so get it while you can :)

      Happy Groundhog Day if you're in the States, and Happy Groundhog Day if you're not!

    • Gavin Phillips
      February 15, 2016 at 2:45 pm

      Hi Joe. Hopefully you've been able to use Fik of borg's Print to PDF idea, which should work absolutely fine. If you don't want to reformat, just head to Print in your browser. You should have at least one option to Print to PDF, and if using Chrome you can Save to PDF for printing, too.

      Good luck.

    • David
      September 11, 2016 at 4:55 pm

      If you're using Firefox, there is a "book" icon in the address bar for a reading view. It strips everything but the article making a clean print.

  14. Bruce Epper
    February 2, 2016 at 7:40 am

    Just a few items here:

    1. The '@' symbol on the @echo off line prevents the 'echo off' command from being echoed to the screen since the setting defaults to 'on'. It can be used in this manner for any particular command and it is not an integral part of the echo command.

    2. In the backup script, the 'set drive=X:Backup' line may produce unexpected results. I know that the pre/code blocks are stripping backslashes, but the paragraph after the code displays that line in the same manner. If the current directory on drive X: is not the root, your Backup folder may not appear where you want or expect it. If the batch file itself resides on the same drive and it is not executed from the root of the drive, the destination definitely will not be in the root. Users are better served using an absolute path rather than a relative path.

    3. ROBOCOPY didn't replace anything. Starting with Windows Vista it has become an integral part of the operating system. Prior to that, it was available only through the Windows [insert version] Resource Kits.

    • Gavin Phillips
      February 15, 2016 at 2:40 pm

      Cheers Bruce, all absolutely right. I've even accidentally a word in the ROBOCOPY explanation.

  15. Hadrian
    February 1, 2016 at 3:03 pm

    What is this? Windows 95?! Go with PowerShell instead!! Fewer commands, commands that make sense and follow similar structure, more flexible, easier to learn.

    • Anonymous
      February 1, 2016 at 3:26 pm


      Both DOS-style batch programming and Powershell have their place. DOS-style more or less means that if you can conceive of the command line routine for doing something, you already know how to to write the batch file for it. It's great for trivial tasks like most of what is presented in this article.

      Powershell makes a lot of sense for someone with more of a programming background, but it also means spending a while looking at the command reference for whatever you're automating. No one has 30+ years of experience writing Powershell. Powershell command execution can also be slow and relatively CPU intensive, which might not be ideal for some situations.

    • nolongerapple
      February 1, 2016 at 10:18 pm

      I like batch. Much easier for simple things.

    • Gavin Phillips
      February 15, 2016 at 2:37 pm

      Hey Hadrian,

      As likefun butnot succinctly illustrated, both have their place, and we are duty bound at MUO to cover all the bases. However, a PowerShell article is on the way, so keep an eye out for that.


    • Mike M
      December 30, 2017 at 10:28 pm

      Power is for anyone who has programming experience. Also, alot of the commands work only when in a domain environment, so most home users do not have access to those commands. Batch runs fast and clean and does not forgive mistakes easily. The article is good but, the backup routine could use alot of work. If one is using a batchfile make use of the environment variables. They hold the users name,home directory,user profile and such.