More NT Command-Script Techniques

Digging deeper from the command line

In the July 1999 Top 10, I shared some useful command-script techniques. In this column, I provide 10 more tips that can add power to your scripts and give them a professional appearance.

10. Use the Cls command to clear the Command Prompt window. Clearing the screen is a good technique to use at the beginning of a script.


9. Use the @title command to give the Command Prompt window a custom title. By default, when a command script runs, it opens a window with the nondescript Command Prompt label, which doesn't provide much information.

@title=<Your Title>

8. Use the @color command to set the color of the Command Prompt window. The @color command accepts a two-digit attribute setting. To see all possible color combinations, enter color/?. The following example sets white on blue:

@color 1F

7. Use the Pushd and Popd commands to save and restore the current directory. To perform actions in different folders, Windows NT command scripts commonly change the current directory. The Pushd command stores the current drive and directory, then changes to the specified drive and directory. Popd changes the current directory back to the directory that the Pushd command stored.

C:\> pushd C:\temp
C:\temp> popd

6. Use the Setlocal and Endlocal commands to protect environment variables. Because environment variables are common to all the tasks that run in the system, changing them can alter your system's functionality. Use the Setlocal command to restrict any environment changes to the current script. The Endlocal command restores the previous environment settings. (These commands work only within a script, not when you type them directly on the command line.)


5. Use the Errorlevel environment to test command results. Many NT commands and other batch-oriented console programs can return information (i.e., error codes) to the calling script that informs you of the program's success or failure. You can use the Errorlevel environment to access this error information (if the program provides it).

net group mygroup myuser /add
if errorlevel 1 echo Net Group Add failed

4. Use quotation marks to work with long filenames. Long filenames make files easier to find. But, if a command accepts more than one parameter, using long filenames can be cumbersome in command scripts.

copy "C:\temp\sales briefing sheet 1.doc"

3. Use redirection for input and output. The following example shows how you would use the dir_in.txt file (which contains the input parameter for the Dir command) to execute the Dir command and send the output to dir_out.txt.

dir < dir_in.txt >dir_out.txt

Use the @findstr command to conditionally take action based on a text string contained in a file. The following example redirects ping output to a file, then checks that file for the phrase timed out and executes the pingerror.cmd script (if the timed out text is present).

ping myserver >ping_output.txt
@findstr "timed out" ping_output.txt && pingerror.cmd

1. Use the For command to parse files (i.e., read a file's contents) and take action based on the contents. The following example shows how to read the contents of a text file containing a list of server names and ping each server:

for /f %%i in (servers.txt) do ping %%I 
TAGS: Windows 8
Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.