HomeHow To

How to Create a Batch File in Windows 10?

This blog post will educate you on how to create a batch file in windows 10, how to run the batch file in CMD, about batch file, basic commands and a few more related things. Continue reading!

“Batch File” Does it sound foreign to you?

If No is your answer, you probably know about it. But, if the answer is Yes, you probably have no idea what it is and what it does?

Don’t worry, in this blog post, I am going to talk Batch File, and you going to learn a lot of things about it. Let’s begin then!

What is a Batch File?

A Batch File is the list of commands that are executed in sequence until all commands are executed. All the commands written in a Batch File are executed by Windows inbuilt utility CLI short for Command Line Interpreter.

You can write the commands in Notepad or Notepad++, and then save the file with the .bat file extension. Saving the file with this extension makes it a batch file which you can double click to execute the commands written in it. It does open in the DOS, and the result is shown.

Generally, people use Batch File to automate the execution of commands that are needed. For example, if you need to execute the DOS command, ipconfig /flushdns to flush DNS which at times improves the internet connection speed to a bit, then what you do is, open Command Prompt, and type the command and hit Enter. And, the command is executed.

But, what if you need to do this a lot of time every day? Won’t you feel lazy to type every time?

Here, a batch file can help you.

How to Create a Batch File in Windows 10?

Open a Notepad, type there ipconfig /flushdns, and save it as anyname.bat. Now, every time you need to flush the DNS, just double click that .bat file, and the command will execute. No more typing all the time. The code should look like this:

Batch File

After the command, I wrote another command “Pause” because when you write it, the screen stays when you double click the batch file. Without Pause command, the screen just opens and gets closed, and you cannot see anything.

run bat file from cmd

This is the result of that commands when executed.

Further, you can do a lot of things such as removing the path shown in the cmd.  I add @echo off at the beginning; then the path will not show in the CMD. The new code should look like this:

@echo off

ipconfig /flushdns

pause

how to run batch file in cmd

See the result now, only the result is showing, and nothing else.

What does “@echo off” means?

Echo Off is added so that when you execute a batch file, it won’t show everything written in the .bat file but only the results.

For example, Here is my code for a .bat file:

title this is my first batch script

echo welcome to the first one

pause

The result will be:

how to create a .bat file windows 10

But, If I add Echo Off to the code:

Echo off

title this is my first batch script

echo welcome to the first one

pause

Then the result will be:

how to create a batch file in windows 10

You can see the difference that when we add Echo off, it shows only the prompt and the executed result. It hides other things from the file.

Now, if you add @ before echo off

@Echo off

title this is my first batch script

echo welcome to the first one

pause

then the result will be:

ow to run batch file in cmd

It hides the prompt as well.

These explain how echo off and @echo off work. Also, what happens when you don’t add echo off.

Also, you have learned how you can write a batch file in Windows 10. These examples are the basic ones; you can write even more complex batch files as per the requirements.

Now, let’s talk about different ways you can run batch files.

How to run Batch File in Windows 10?

As I described earlier in this post that when you write commands in a text file, and then save that with extension .bat anywhere on your computer, you will see the batch file is created which you can double click to run. When you double-click a batch file, it opens DOS in which you see the commands that are written in the file are executed. Moreover, you can right click on the file icon and select Open as well to run .bat file in Windows.

How to run Batch File in CMD?

Though the easiest option is to double click the .bat file to run it, you may have been working within Command Prompt, and want to run a batch file there, then you can run from there as well.

You need, go to the directory in which the batch file is saved. To change the directory in DOS, you can use CD command. Once you are into that directory or location, just type the batch file name, and hit enter to run that. For example, I have atish.bat file on my desktop, so first I opened Command Prompt, moved to the location which is desktop using CD command, and then typed the file name atish {you don’t need to type it with extension here}.

This is how you can run bat file from CMD.

I am sure you have some ideas about batch files now. However, the examples are very basic; you can write complex batch files as well.

Some of the basic commands for Batch Files:

ECHO: To display text.

ECHO OFF: It hides the texts. And, only shows prompt and the executed result

@ECHO OFF: It hides both texts and prompts, and only shows the executed result.

XCOPY: To copy files with extra options

START: To run a file with its default application

FOR/IN/DO: To specify files

MKDIR: To create directories

REM: To enter a comment line in the program

RMDIR: To remove directories

DEL: To delete files

COPY: To copy a file or files

TITLE: To edit the title of the window

Further, in this post, I am going to share some cool batch files that you can try out.

Few cool Batch Files that you can try and enjoy

  1. Batch file to shut down your Windows 10 Computer

echo off

msg * computer will shut down quickly

shutdown -c “sleep well” -s

Copy the above code, and paste in notepad, and save as shutdown.bat (you can free to give it any name. Just make sure you save it with .bat extension). Once the file is created, you can run that by double-clicking. And this batch file will force your computer to shut down quickly.

Moreover, you can also set the time to schedule the shutdown after the batch file is executed, and for that, you need to add –t (time in seconds)

So the new code will be

echo off

msg * computer will shut down quickly

shutdown -c “sleep well” -s –t 1000

You can enter anytime you want.

Excellent Trick to Abandon the shutdown: Some of your friends might want to surprise you with this and will tell you to stop this shutdown process within 1 minute by giving 1 minute, and you won’t be able to stop it because you might not stop it easily. But, there is a code, that you just type in Run, and hit enter, and the code is shutdown –a or shutdown /a. This abandons the scheduled shutdown. However, you can do this only when you get a few seconds to stop it. If there is no time given in the code, the process will instantly shut down the computer. Moreover, if you are fast enough you can still type this fast and enter or if you have a .bat file for this command already, you can just double-click that to abandon shutdown.

Never put this batch file at Windows startup especially when you don’t give there any time in the batch file because then your computer will quickly shut down after starting up. 

  1. Print Random Numbers in Matrix Style

If you have watched the movie Matrix, you must have watched the string of green numbers/characters appearing a lot of time. You can do the same using Batch File as well.

@echo off

color 02

:start

echo %random% %random% %random% %random% %random% %random% %random% %random% %random% %random%

goto start

Type the above code in the Notepad, and save that as matrix.bat. Now, run that batch file on your Windows computer. You will see this:

The numbers will keep generating.

You can find a lot of cool, useful and funny Batch file codes on the internet.

Final Thoughts

Batch files are very useful at times. All you need to know are the commands that you have to add in the file, and use it when required. Moreover, you can find readymade batch file codes on the internet for various purposes.

I have been using a lot of batch files to carry out specific tasks on my Windows 10 PC. I am sure; this blog post has given you a better idea of how to create a batch file in Windows 10, how to run the batch file in CMD and a few other related things.

Let me know your views in the comments.

Comments (9)

  • Atish – I work with BAT files almost every day at work and this article is helpful for people like me. Thanks for this write up.
    Praveen Rajarao recently posted…10 Expert Tips To Make an Oily Skin Look GoodMy Profile

    Reply
  • Hi bro,
    Feels like magic :0

    I had heard about batch files but never gone deep or tried them. I loved how so many things can be done using batch files and more than anything else its a cool factor that can be added.

    I loved how you wrote the detailed post, I must say this is the only post one would need.

    Thanks for sharing with us and as a reader I would request you to add some fun and interesting “full guides” more to this blog.

    This will make the boring “freelance life” a better life to live 😀

    Best,
    -Swadhin
    Swadhin Agrawal recently posted…Top 10 Best Search Engines In The World [Google Alternatives 2018]My Profile

    Reply
  • Hi Atish,
    I wasn’t aware of the batch files, but after reading your post it seems very easy to learn.

    Thanks
    Karan

    Reply
  • Hello,

    Thanks for sharing this beautiful article.

    After reading this article… I can say creating batch file is pretty easy in windows 10 😉

    Thanks.
    Bhargav recently posted…The Ups And Downs Of Transitioning Into Your New Life AbroadMy Profile

    Reply
  • Thanks for sharing this amazing article..i learnt a lot from this.But can you resolve a issue i am facing.

    While on Windows 7, I’ve created a lot of batch files (.bat) to automate some tasks.

    Now that I’ve moved to a different machine with Windows 10 (we’ve skipped Win8), I’m having some issues with them.

    My domain user was added to the machine’s local Administrators Group and I’ve disabled UAC (just as I had on Win7).

    To be able to control Services (net stop, net start), the Command Prompt must be running as Administrator. This can be achieved by setting an advanced property in a shortcut to the batch file (quite annoying since I have many), but it won’t work if I want the batch file to run on startup (in the Startup folder of the Start Menu) — the batch file will just not start if “Run as administrator” is set on its shortcut.

    I can’t seem to find a solution to this problem – what I find is either about Win7, require some additional commands in the batch files (such as runas) or giving rights to specific services. I don’t think I had to jump thru any hoops in Win7 for this to work, but it’s been a while.

    if you can help me with this issue…

    Reply
  • For some reason sometimes Alt +F4 stops working, so i have to press shutdown button.
    i was wondering if i can do that by creating a batch file writing some commands in it and then i will keep it on desktop and everytime i want to shutdown i can click it.
    Thanks Atish !

    Reply
  • hello,
    thanks for sharing the informative article… before reading this article
    I don’t know how to create the batch file and After reading this article… I can say creating batch file is pretty easy in windows 10
    Gaurav gupta recently posted…By: DwightXMy Profile

    Reply

Leave a Reply

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

All the data shown above will be stored by Techtricksworld.com on https://www.techtricksworld.com. At any point of time, you can contact us and select the data you wish to anonymise or delete so it cannot be linked to your email address any longer. When your data is anonymised or deleted, you will receive an email confirmation. We also use cookies and/or similar technologies to analyse customer behaviour, administer the website, track users' movements, and to collect information about users. This is done in order to personalise and enhance your experience with us.

CommentLuv badge