Category Archives: Basics

Batch Files Are Still Valuable

Here we are in 2017 and as I think about the past I realize that the future still holds much valuable use of batch files. Microsoft introduced the VBS-based Windows Scripting Host (WSH) in the late 90s. Then PowerShell and yet Command Prompt commands and, therefore, batch files still live on. The simple fact is that batch files are still much easier to create and edit than WSH scripts or PowerShell scripts. This does not mean that PowerShell lacks value. It simply means that, when you want a quick solution to automation, batch files work well – usually very well.

Just a few example uses of batch files are:

  • Automating local user account and group management with NET USER and NET GROUP
  • Backing up closed files to an external drive or network share
  • Generating reports related to network operations
  • Configuring complex networking parameters automatically

Many, many more could be listed. To prove that the Command Prompt really isn’t going anywhere, Microsoft gave us a new tool with Windows 10 called MpCmdRun.exe. This command is used to execute Windows Defender scans from the Command Prompt. Adding yet another useful batch file example, malware scanning.

For example, the simple command mpcmdrun -scan -scantype 1 executes a quick scan from the Command Prompt. The command provides a return code of 0 when no malware is found. Consider, then, the coupling of the previous command with an if errorlevel == 0 (echo Success) else (echo Failure). The end result is the output of the word Success when no malware is found or Failure when malware is found.

Now, this example is not extravagant, but imagine if the if statement called other commands or applications to take action. As you can see, indeed, batch files are still valuable and powerful. Even in Windows 10, the latest and greatest from Microsoft.

MpCmdRun.exe example

 

Pushd

The Pushd command stores the current directory for use by the popd command, and then changes to the specified directory. The popd command changes the current directory to the directory that was most recently stored by the pushd command

Syntax (view syntax formatting guide):

pushd [<Path>]

Syntax Explanation

 

  • <Path>: Specifies the directory to make the current directory. This command supports relative paths.
  • /?: Displays help at the command prompt.

Every time you use the pushd command, a single directory is stored for your use. However, you can store multiple directories by using the pushd command multiple times.
The directories are stored sequentially in a virtual stack. If you use the pushd command once, the directory in which you use the command is placed at the bottom of the stack. If you use the command again, the second directory is placed on top of the first one. The process repeats every time you use the pushd command.
You can use the popd command to change the current directory to the directory most recently stored by the pushd command. If you use the popd command, the directory on the top of the stack is removed from the stack and the current directory is changed to that directory. If you use the popd command again, the next directory on the stack is removed.

If command extensions are enabled, the pushd command accepts either a network path or a local drive letter and path.

If you specify a network path, the pushd command temporarily assigns the highest unused drive letter (starting with Z:) to the specified network resource. The command then changes the current drive and directory to the specified directory on the newly assigned drive. If you use the popd command with command extensions enabled, the popd command removes the drive-letter assignation created by pushd.

Explanation with Examples

The following example shows how you can use the pushd command and the popd command in a batch program to change the current directory from the one in which the batch program was run and then change it back:

@echo off
rem This batch file deletes all .txt files in a specified directory
pushd %1
del *.txt
popd
cls
echo All text files deleted in the %1 directory


 

Prompt

The Prompt command changes the Cmd.exe command prompt. If used without parameters, prompt resets the command prompt to the default setting, which is the current drive letter and directory followed by the greater than symbol (>). A command prompt (or just prompt) is a sequence of (one or more) characters used in the  command-line interface to indicate readiness to accept commands. By using any of the set of character combinations listed belown the command-prompt can be modifiable by the user. Depending on the environment, a user may be able customize the command-prompt by changing the color, adding special character that may be conducive to their liking or perhaps adding the current time, all to make the prompt more informative or visually pleasing.

Syntax (view syntax formatting guide):

prompt [<Text>]

Syntax Explanation

  • Text: Specifies the text and information that you want to include in the command prompt.
  • /?: Displays help at the command prompt.

You can customize the command prompt to display any text you want, including such information as the name of the current directory, the time and date, and the Microsoft Windows version number.

The following is a list of character combinations that you can include instead of, or in addition to, one or more character strings in the Text parameter. The list includes a brief description of the text or information that each character combination adds to your command prompt. 


  • $q: = (equal sign)
  • $$: $ (dollar sign)
  • $t: Current time
  • $d: Current date
  • $p: Current drive and path
  • $v: Windows version number
  • $n: Current drive
  • $g: > (greater than sign)
  • $l: < (less than sign)
  • $b: | (pipe)
  • $_: ENTER-LINEFEED
  • $e: ANSI escape code (code 27)
  • $h: Backspace (to delete a character that has been written to the command line)
  • $a: & (ampersand)
  • $c: ( (left parenthesis)
  • $f: ) (right parenthesis)
  • $s: space

NOTE: When command extensions are enabled (that is, the default) the prompt command supports the following formatting characters:

  • $+: Zero or more plus sign (+) characters, depending on the depth of the pushd directory stack (one character for each level pushed).
  • $m: The remote name associated with the current drive letter or the empty string if current drive is not a network drive.

NOTE: If you include the $p character in the text parameter, your disk is read after you enter each command (to determine the current drive and path). This can take extra time, especially for floppy disk drives.

Explanation with Examples

To set a two-line command prompt with the current time and date on the first line and the greater than sign on the next line, type:

prompt $d$s$s$t$_$g

The prompt is changed as follows, where the date and time are current:

Fri 06/01/2007  13:53:28.91

>

To set the command prompt to display as an arrow (–>), type:

prompt –$g

To manually change the command prompt to the default setting (the current drive and path followed by the greater than sign), type:

prompt $p$g

 

Print

The Print command is used to add text files to a print queue. Because of limitations of text files it is not equal to the LP or LPR commands. The sole purpose of the Print command is simply to send a text file to a printer.

Syntax (view syntax formatting guide):

Print [/d:<PrinterName>] [<Drive>:][<Path>]<FileName>[ ...]

Syntax Explanation

  • /d:<PrinterName>: Specifies the printer that you want to print the job. To print to a locally connected printer, specify the port on your computer where the printer is connected.
    • Valid values for parallel ports are LPT1, LPT2, and LPT3.
    • Valid values for serial ports are COM1, COM2, COM3, and COM4.

You can also specify a network printer by using its queue name (\ServerNamePrinterName). If you do not specify a printer, the print job is sent to LPT1 by default.

  • <Drive>: Specifies the logical or physical drive where the file you want to print is located. This parameter is not required if the file you want to print is located on the current drive.
  • <Path>: Specifies the location of the file you want to print. This parameter is not required if the file you want to print is located in the current directory.
  • <FileName>[ …]: Required. Specifies the file you want to print. You can include multiple files in one command.
  • /?: Displays help at the command prompt.

Explanation with Examples

To send the file Report.txt in the current directory to a printer connected to LPT2 on the local computer:

print /d:lpt2 report.txt

To send the file Report.txt in the c:Accounting directory to the Printer1 print queue on the \CopyRoom server:

print /d:\copyroomprinter1 c:accountingreport.txt

 

Popd

The popd command changes the current directory to the directory that was most recently stored by the pushd command. The pushd command saves the current working directory in memory so it can be returned to at any time.

Syntax (view syntax formatting guide):

popd

Syntax Explanation

  • /?: Displays help at the command prompt.

Every time you use the pushd command, a single directory is stored for your use. However, you can store multiple directories by using the pushd command multiple times.
The directories are stored sequentially in a virtual stack. If you use the pushd command once, the directory in which you use the command is placed at the bottom of the stack. If you use the command again, the second directory is placed on top of the first one. The process repeats every time you use the pushd command.
You can use the popd command to change the current directory to the directory most recently stored by the pushd command. If you use the popd command, the directory on the top of the stack is removed from the stack and the current directory is changed to that directory. If you use the popd command again, the next directory on the stack is removed.

When command extensions are enabled, the popd command removes any drive-letter assignations created by pushd.

Explanation with Examples

The following example shows how you can use the pushd command and the popd command to save and return to a current working directory:

C:usersTom>pushd /

C:>pobd

C:userTom>

 

Syntax Formatting Guide for WindowsCommandLine.com

This document is a guide to the syntax representations shown here at WindowsCommandLine.com. It is intended to help you understand the syntax shown for each command.

The following example syntax statement, taken from the icacls command,  will be used to explain the  syntax elements you may see for a command:

icacls FileName [/grant[:r] Sid:<Perm>[...]] [/deny Sid:Perm[...]]
 [/remove[:g|:d]] Sid[...]] [/t] [/c] [/l] [/q]
 [/setintegritylevel Level:Policy[...]]

OR

icacls Directory [/substitute SidOld SidNew [...]]
 [/restore ACLfile [/c] [/l] [/q]]

The first parameter in the syntax is always the command itself. In this case, it is the icacls command. This command is used to manage permissions from the Command Prompt.

When you see italicized parameters, these represent variables. For example, the parameter FileName should be replaced with an actual filename and the parameter Directory should be replaced with an actual directory of folder name, such as:

icacls file1.txt

The preceding command would display the current permissions on the file named file1.txt.

Parameters inside of square brackets are optional. For example, with the icacls command, you can use the /t switch if you need it, but it is not required for the command to process.

When you see […], this indicates that you can repeat the previous parameter as often as needed in the command. For example, when using the icacls command to grant permissions, you can do so for multiple SIDs (security IDs) at the same time.