Corrected incorrect information on step 2 (to do with limitations with parameters).

Added a couple of lines about an auto-installer in step 4.

This instructable will cover the basics of creating your own commands (and even installing them on others computers) which you can use to further cut down the amount of work you need to do.

Some things you should have:
  • A Windows computer (preferably modern)
  • A USB (for transporting the files around)
  • A basic to advanced understanding of the syntax involved with batch commands (this is not a tutorial to help you learn about commands, this is a tutorial about making your own
  • Basic computer know how (for instance, knowing how to navigate through folders)

If you don't have a basic understanding of the syntax involved with batch scripts and the like, feel free to either search for a tutorial, ask questions or comment on something you don't understand.

Step 1: What to Make?

Before you create your world-famous new command, you need to figure out what to make.

Most commands have one specific purpose (such as the REN command), and each switch adds some more functionality to that command. Most of these commands were made because it would be impossible to do a certain task (that batch files are commonly used for) without them. Unfortunately, you cannot make a command that will make something possible that has never been possible before (because if you're making it, it's possible). So, the best thing to base your command around is to save time. 

So, ask yourself, What do I do frequently that takes a long time to program in? 

If you're not that much of the typical "hardcore programmer" (you're using batch here, so not many of you will match that term, it was purely to get my point across), then you might not need a function to save time, because you don't do that much.

In that case, ask yourself something along the lines of Which command lacks something?

That basically means, what command are you not happy with and you think you can do better?

Unfortunately, you can't just modify the code within the commands, so you have to resort to making a new command (such as SETX).

I probably should have explained how the command line (cmd.exe) parses commands. Well, looks like I found my topic for the next section.

Step 2: How Does Cmd.exe Parse Commands?

This section basically covers what happens when you put something like "echo Hello". Unfortunately (I use that word too much), I don't know the code they use for it, so I can't exactly be perfectly accurate with this. Besides, this is just to demonstrate what happens when you type in a command (you probably shouldn't skip this if you don't know already). 

So, the first thing cmd does is search for a file named whatever the command (capitalization doesn't matter) was (let's use echo for this situation), with the extension of either .exe, .bat, .cmd or .com (I'm pretty sure there is either less or more extensions that it searches for). Once the file is found, whatever you put after it is sent to the script in the form of a parameter (these will be explained below).

So, if I put in a fictional command such as "EAT hamburger fries", and there is a file named EAT.bat located in the appropriate folder (that will be explained later too), then it send the words "hamburger" and "fries" as parameters 1 and 2 (respectively). 

Let's say this is the code in the file EAT.bat

@echo %USERNAME% ate %1 and %2

Very short code, isn't it. Anyway, if I wrote "EAT hamburger fries" then the output would be
Prof Pickle ate hamburger and fries

It's a modern masterpiece, isn't it? If you're wondering where the variable "USERNAME" came from, it's a pre-set variable that holds the currently logged in user's username. Hint: To find all the pre-set variables and their values, type "SET" in the command line window.

As you may have guessed already, there can only be 10 parameters*. Wait, did he make a typo?

You heard correctly, there are 10 parameters: %1, %2, %3, %4, %5, %6, %7, %8, %9 and a special, pre-set parameter by the name of %0. This special parameter holds the file path of the file.

Here's an experiment for you to try:
  1. Create a new folder in your Documents named Test (if there isn't one already)
  2. Open up Notepad and type in the following...
@echo off
echo %0

   3.   Save it as "parameters_test_01.bat" (you can save it as whatever you want, It won't make any significant difference
       4.   As you may have already guessed, you now need to double click on it.

What you should see is the file path of the file echoed on the screen. Fancy, huh?

I do think that about sums it up (knowing me I'll leave out some vital piece of knowledge that will determine the success of failure of your life).

* There can be more than 9 user-set parameters, but you can only access 9 of them. Using the shift command, you can move the value of a parameter to the parameter before it. Here's an experiment you can try...

@echo off
echo Press any key to send the following parameters to a command:
echo 1 2 3 4 5 6 7 8 9 10
call :command 1 2 3 4 5 6 7 8 9 10
echo The script has returned from the command.
echo Press any key to exit

echo Parameters 1-9:
echo %1 %2 %3 %4 %5 %6 %7 %8 %9
echo The shift command will now be used to shift the values.
echo Parameters 1-9:
echo %1 %2 %3 %4 %5 %6 %7 %8 %9
goto :eof

Step 3: Let's Actually Do Something!

Now's the time to actually do something. 

So, first you need to decide on the syntax of your command. I will be making a command to echo a random number between parameter one (max) and parameter two (min).

So, here's the code:

@setlocal enabledelayedexpansion
@echo off

if [%1] equ [/?] (
echo Echoes a random number.
echo Usage: RANDOM max min
echo "max" in the maximum number and "min" is the minimum.
echo Max cannot exceed 32767.
exit /b 0

set max=%1
set min=%2

if %min% geq %max% (
echo Minimum number is less than or equal to maximum number.
exit /b 1

set max-=min
set /a num=%random% %% %max% + %min%
echo %num%

So, what's so useful about a command that only echoes the result?

Well, so long as a code has an output, you can set it as a variable. So, here is a little "program" I made up.

@echo off & setlocal enabledelayedexpansion

echo Welcome to Prof Pickle's amazing game of luck!
echo Just press any key to roll the dice and try to get a 7!
echo Press any key to start!

if defined val echo Last role: %val%
echo Pres any key to roll the dice.
for /f %%i in ('random 6 1') do set dice1=%%i
for /f %%i in ('random 6 1') do set dice2=%%i
set /a val=dice1+dice2
if %val% neq 7 (echo LOSER!) else echo WINNER!
echo Dice #1 - %dice1%
echo Dice #2 - %dice2%
echo Total - %val%
echo Press any key to play again
goto game

So, that game isn't going to win any Academy awards, but it's just an example. So, what will you do to utilize your new command?

Wait! Not so fast. Before you use your command, you need any batch file (including the command prompt itself), you need to make it accessible. Keep reading to find out.

Step 4: Installation

Ok, so you have your command, but how do you use it? Well, you need to "install" it on your computer. You can make an auto installer, but you need to have admin permission. The basic gist is you copy the file from your USB into their Windows System32 folder.

So, either you can make an auto installer (which I won't be doing), or you can drag and drop your file into your C:\Windows\System32 folder within your computer.

Note: If you make a script that is intended to be used by more than yourself, you should put your new command in the same folder as the script, that way it will still work.*

* It will work so long as it's in the current directory.

So, I think that about wraps it up.

If you're looking for some pre-made functions (made by me), see https://www.instructables.com/id/Dos-Functions/

<p>Great tut. In your command code, do you need the <em>@setlocal enabledelayedexpansion thing? Or can you change the enabledelayexpansion? It would be great if you answer.</em></p>
Nice tutorial, but your little dice game is weird. It didn't work for me, I had to change &quot;random&quot; to &quot;%random%&quot; but then, I still couldn't figure out the problem
Did you have the other file in the same directory (random.bat)?
<p>Hey, Prof. Pickle, since Cmd.exe uses other files for commands (if u don't get what i'm saying, I am basiclly (spelled wrong) saying the cmds aren't built into command prompt) then wouldn't you be able to find the files it uses and edit them?</p><p>I know they would be invisible but you can make all files in windows visible by tinkering with the folder options&gt;view settings. </p><p>Please reply - Jax B.</p>
<p>Most commands that the command prompt uses are in executable format, written in C++. You can re-write the command, compile it and replace the original, but other than that...</p>
This is very useful for me. Thank you sir. <br> <br>can you tell me how to set help utility for my own command???
I like to put it in sort of an internal &quot;function&quot; which I'd call when they didn't do something right (regarding to arguments). Here's an example: <br> <br>@setlocal &amp; @echo off <br>if %1 equ &quot;&quot; goto :help <br>::code <br> <br>:help <br>echo This is how you use this command...
This I knew. But what about when someone(user) type &quot;Help &quot; ???
If they put it in as an argument (example: command help, command /?) <br> <br>@setlocal &amp; @echo off <br>if &quot;%1&quot; equ &quot;help&quot; goto :help <br>if /i &quot;%1&quot; equ &quot;/?&quot; goto :help <br>::code <br> <br>:help <br>::info
Very nice tutorial! This was very informative! I will be sure to try it.
Glad you liked it. Remember to share what you make!
I haven' made anything yet, I might later on today, right now I am in the middle of starting a text-adventure game. I haven't done batch in so long... I finally recovered my files from my old computer and I saw this tutorial and it got me into batch again. :D
Great tutorial! out of my head..surely I will try it....
Glad you liked it.

About This Instructable




Bio: I enjoy anything to do with computers. I have made some interesting scripts, and I am constantly improving my skill set. One day I hope ... More »
More by Prof. Pickle:How to Create your own Commands with Batch Start a batch file completely hidden Tips on how to find errors in code (focused on DOS) 
Add instructable to: