The Basics of CMD and Batch

Updated on February 8, 2018
Beigevenus profile image

Clive is a self-taught programmer with years of experience using CMD and writing Batch scripts combined with VBScript and Powershell

What is CMD and Batch?

CMD is currently the default command line interpreter of Windows, slowly being replaced by Powershell, but more on that another time.

CMD is used for simple things like displaying your device's IP address or for more advanced tasks as wiping entire hard drives.

A Batch script is basically the commands of CMD put together to run in a certain order as a script. This can, like the commands, be used for simple things or more complicated purposes (An example of my own being a poorly optimized, but still working, chat program).

An example of a simple command in CMD, the 'echo' command
An example of a simple command in CMD, the 'echo' command
An example of a simple Batch script outputting "I am Batch!"
An example of a simple Batch script outputting "I am Batch!"

Why use Batch?

You might be thinking: "Why use such a limited language when C++ and Java exist?", and it's an excellent question!

I have my own personal reasons for using Batch in most cases, but here is some general advantages:

  1. Batch is very simple to learn
  2. Batch scripts can be run on every Windows system without having to install anything at all (not that this doesn't apply to other languages)
  3. It's fast to use if you just want to make a simple tool

Powershell is also an excellent choice, however, it's a bit more complicated to wrap your head around at times.

If you're new to CMD, using the help command can really make you want to actually scream for help
If you're new to CMD, using the help command can really make you want to actually scream for help

So, how does this work?

Well, first of all, you should smack that Windows key and then r (Win+r) to bring up Run, type in 'cmd' and hit enter.

This will present you with the classic black and white (sometimes green, depending on your system) command prompt. From there on, you can run the 'help' command as I did in the picture above and study some of the commands.

For more detailed descriptions of each command, enter the name of the command followed by '/?', for example:

ipconfig /?

If you're not comfortable with the way CMD itself explains it, you can head over to this site for even more details and examples on how to use the commands. If you're still not following, there are tons of other websites and forums explaining the commands of CMD, you're free to contact me as well if you ever need any help!

Examples for the 'ipconfig' command on SS64
Examples for the 'ipconfig' command on SS64

What do I need to get started?

That's what I like the most about Batch, you need absolutely nothing to get started. Unlike C++ you don't need Visual Studio or something like that, you can write Batch scripts in Notepad if that's what you want to do.

Of course, tools exist to make scripting easier for you, like Notepad++.
I highly recommend getting this program no matter what language you're writing in as it supports a lot of them, including Batch. It's super convenient as it uses different colors for different types of commands, making your script easier to navigate.
So, if you feel like it, go download Notepad++ as it'll make things much easier! You can get it here.

As I have a couple of personal preferences when writing Batch scripts, I've made this little list for you to seek inspiration in when preparing to write scripts:

  • A text editor (Preferably Notepad++)
  • A directory structure that's easy to navigate
  • Proper naming of your files as it will make it much easier to find again
  • Some good music to help you concentrate (If you prefer silence when scripting, that's totally fine as well)
  • A drink, as you can spend a lot of time with this (Just five more minutes!)

Just five more minutes!

— Clive Hicks

Creating a Batch script

First of all, we have to tell Batch that we don't want it to tell us when it executes a command. We do this by putting this on the first line:

@echo off

This will get rid of unnecessary output.

DISLAIMER:
Leaving echo on can be very useful for debugging, but as a newbie, I recommend turning it off to avoid confusion.

After this, we can start writing our script.

The output gets a bit confusing when echo is on
The output gets a bit confusing when echo is on

I highly recommend playing around with the commands in CMD, but beware! Some of the commands can really mess up your system if you're not careful. You could end up deleting entire drives if you don't know what you're doing.

Please play respectfully, my friends. Make sure that you know exactly what the command can do before using it.

With that said, let's move on to a couple of simple commands you can use to make your script do something:

Command
Syntax
Function
echo
echo "message"
Outputs whatever you put inside the quotation marks
title
title "string"
Changes the title of the command window to whatever you want
color
color [attrib]
Changes the fore- and background colors of the command window (Please refer to the 'color /?' command for color codes)
goto
goto [label]
Jumps to the label with the name specified
label
:[label]
This is what the goto command jumps to if the name of the label is the same
cls
cls
Clears the screen of any output
timeout
timeout /t [time]
Waits the specified amount of seconds
del
del [filename]
Deletes the specified file
pause
pause
Waits for the user to press any button before continuing
exit
exit
Closes the application
These commands are very simple, but it's a good place to start

Finishing a script

When you're finished writing your commands in a script, all you have to do is save it as a Batch file, with the extension .bat
It's really that simple! Now you should be able to double-click your new file and it should run perfectly (granted there are no errors of course).

Example of a Batch script (Base64 Encoder)

@echo off

:start
cls
echo Welcome to the EPT Base64 Encoder and Decoder
echo.
echo Please choose an option:
echo.
echo 1. Encode
echo 2. Decode
echo 3. Exit
echo.
set /p ch=: 
if %ch%==1 goto encode
if %ch%==2 goto decode
if %ch%==3 exit
echo.
echo Invalid input
timeout /t 2 /nobreak>nul
goto start

:encode
cls
echo Please enter the string you want encoded with Base64:
echo.
set /p enc=: 
echo %enc%>input.txt
certutil -encode input.txt output.txt
cls
echo Your string encoded with Base64:
echo.
type output.txt
del output.txt
del input.txt
echo.
echo Press any button to return to menu...
pause>nul
goto start

:decode
cls
echo Please enter the string you want decoded:
echo.
set /p dec=: 
echo %dec%>input.txt
certutil -decode input.txt output.txt
cls
echo Your string decoded:
echo.
type output.txt
del output.txt
del input.txt
echo.
echo Press any button to return to menu...
pause>nul
goto start

Okay, I admit that this one doesn't seem simple at all at first, but bear with me.

When you break it down it's actually pretty simple, just a bunch of echo's and goto's.
Let me quickly explain some of the other commands used here: (The following commands can do more than explained here, however, they are explained with the script above in mind)

  • set /p [variable] - The set command with the /p parameter lets the user enter something and sets the variable to that.
  • if [variable]==[value] - The if statement checks if a variable is equal to a given value or another variable
  • type [filename] - This one just outputs the contents of a file
  • certutil - This command is a bit complicated and I won't go into too much depth with it, but basically, it's able to encode and decode strings (In this case Base64)

The menu of the above script, the Base64 Encoder/Decoder
The menu of the above script, the Base64 Encoder/Decoder

Last words

To finish things off, I would just like to thank you for reading my basic guide on CMD and Batch scripts and I hope that I'll get to write more about this subject in the future as it's pretty much my main hobby.

If you have any questions, or if I've been unclear in any way, feel free to contact me, either through the comments at the bottom of the page or through my account. I'm always willing to help!

A little poll to end on

What programming language is your favorite to use?

See results

Questions & Answers

    Comments

      0 of 8192 characters used
      Post Comment

      No comments yet.

      working

      This website uses cookies

      As a user in the EEA, your approval is needed on a few things. To provide a better website experience, turbofuture.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

      For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://turbofuture.com/privacy-policy#gdpr

      Show Details
      Necessary
      HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
      LoginThis is necessary to sign in to the HubPages Service.
      Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
      AkismetThis is used to detect comment spam. (Privacy Policy)
      HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
      HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
      Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
      CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
      Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
      Features
      Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
      Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
      Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
      Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
      Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
      VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
      PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
      Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
      MavenThis supports the Maven widget and search functionality. (Privacy Policy)
      Marketing
      Google AdSenseThis is an ad network. (Privacy Policy)
      Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
      Index ExchangeThis is an ad network. (Privacy Policy)
      SovrnThis is an ad network. (Privacy Policy)
      Facebook AdsThis is an ad network. (Privacy Policy)
      Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
      AppNexusThis is an ad network. (Privacy Policy)
      OpenxThis is an ad network. (Privacy Policy)
      Rubicon ProjectThis is an ad network. (Privacy Policy)
      TripleLiftThis is an ad network. (Privacy Policy)
      Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
      Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
      Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
      Statistics
      Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
      ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
      Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)