Command Prompt batch scripts have extension .cmd
or .bat
, the latter for compatibility reasons.
To create a hello-word-script, you first need a place where to type it. For simple scripts, also the Windows Notepad will do. If you are serious about shell scripting, you need more effective tools. There are anyway several free alternatives, such as Notepad++.
In your designated editor type:
echo Hello World
pause
Save it as hello.cmd
If you are using "Notepad" as an editor, you should pay much attention to the saved name, as Notepad tends to add always a .txt
extension to your files, which means that the actual name of your file might be hello.cmd.txt
. To avoid this, in the save dialog box:
File name
field enter the name in double quotes, e.g. "hello.cmd"
Save as type
field select All Files, instead of the default Text Document option.If the file has been saved properly, its icon should be similar to (Windows Vista):
You may also consider to disable the option "Hide extension for known file types" in File Explorer folder view options. In this case, file names are always displayed with their extensions.
To execute hello.cmd
there are two possibilities. If you are using the Windows graphical shell, just double click on its icon.
If you want to use the Command Prompt itself, you must first identify the directory where you saved hello.cmd
.
In this regard, if you open File Explorer with +E. In the windows listing files, you normally read the name of the directory path containing them. You can therefore identify the directory of hello.cmd
. Windows directory names tend to be quite long and typing them is error prone. It is better if you select and copy the directory path in the clipboard for later pasting.
Start the Command Prompt. You read a line similar to this.
Microsoft Windows [Version ...]
(c) ... Microsoft Corporation. All rights reserved.
C:\Users\...>
The version/year of Windows of course depends on yours.
In the the final line, before >
, you read the path of the directory which is current. You should make current the directory where your script is. For this reason enter the change directory command cd
, using a line similar to the following:
cd <dirpath>
Instead of <dirpath>
, paste the name of the directory you previously copied.
To paste the directory path, in Windows 10, you just need to type Ctrl-C, as you would in an editor.
For older systems you should be able to do this by right clicking in the cmd
window.
After entering the command, note that current path, before >
, changes accordingly.
You can now run your hello script by simply entering:
hello
The script prints an output similar to:
C:\Users\...>echo Hello World
Hello World
C:\Users\...>pause
Press any key to continue . . .
The lines hosting the symbol >
restate the script instructions as if you had entered interactively.
This can be disabled writing:
@echo off
as the first line of your script. This might reduce the clutter, but you have less hints on what is going on, with respect to those script commands that do not give visible outputs.
The last command, pause
, prompts you to hit any key. When you do, you exit hello
.
If you run hello
from the console, you don't really need it, because, when hello
terminates its execution, cmd.exe
remains open and you can to read hello
output.
When double-clicking in Explorer, you start cmd.exe
for the time necessary to execute hello
. When hello
terminates, cmd.exe
does the same and you have no possibility to read hello
output.
pause
command prevents hello
from exiting until you hit a key, which gives also the possibility to read the output.
Finally, despite the name of the script is hello.cmd
, it is not necessary to type the whole name, its hello
stem is sufficient. This mechanism works for executables too, with extension .exe
. What if there is a script hello.cmd
and an executable hello.exe
in the same directory? The former has priority in the Command Prompt, so hello.cmd
will be executed.