Featured post

The Command Prompt Lives

The Windows Command Prompt, which is the proper name of the Windows command line interface, is still wildly popular. Did you know that more than 1.5 million Google searches are performed each month related to Command Prompt commands? It is estimated that more than 2.5 million total searches are performed across all search engines. This shows a lot of interest in the windows command line and this site is here to help you learn and master each and every command.

The point is that the Windows command line is alive and well. The good news for those who may be old hats with the Disk Operating System (DOS) of years gone by is that the majority of the DOS commands still work in the Windows command line. In fact, the Command Prompt (which is launched with the CMD.EXE program) looks just like the old DOS interface.

In addition, you can still create batch files at the windows command line. These batch files still support the GOTO command and other logic constructions to create advanced scripts for automation of administration tasks. And, yes, you can still find programs that will compile the batch file into an executable so that others cannot easily tamper with your logic.

Now, if you’re completely new to the text-based interfaces, that’s no problem. This site will get you up to speed quickly. Here at WindowsCommandLine.com, we are building a site that will teach you how to use every single command that comes with every version of Windows from Windows XP through to Windows 7 and even those commands that are only on the Windows Server platform. You’ll also learn about batch file programming, interacting with the command line and configuring the Command Prompt application to your liking.

Watch for content to start arriving in April. The site will have complete coverage of all commands by mid-Summer. Thanks for stopping by.

-Tom Carpenter

Execute Batch File Commands During Specified Hours

OK. You want to run a command a certain way if it is between 9 AM and 12 PM. You want to run it a different way if it is between 12 PM and 3 PM. How do you accomplish this with a batch file?

The answer is nested IF statements and the %time% system variable. The %time% variable stores the time in a 24 hour format including hour, minute, second and millisecond:

09:02:45.80

This represents 9:02 AM at 45.80 seconds into the minute. I prefer to work with just the hour in situations like this. So I create a variable called %hour% for such batch files using the following command:

SET “hour=%time:~0,2%”

The following shows this operation at the Windows command line:

Extracting the hour from the time variable

It is also important to know that you have operator keywords used with the IF statements. They are as follows:

  • EQU – equal
  • NEQ – not equal
  • LSS – less than
  • LEQ – less than or equal
  • GTR – greater than
  • GEQ – greater than or equal

Using these keywords can be simpler to read than the traditional == or != operator types for the uninitiated who read your batch file.

With this in mind, the following batch file code will allow you to test for the hour range and run commands only during specified hours:

@ECHO OFF
SET hour=%time:~0,2%
IF %hour% GEQ 9 IF %hour% LEQ 12 (GOTO RUNIT)

ECHO Wrong time to run!
GOTO END

:RUNIT
ECHO Right time to run!

:END

Hopefully this helps you in such scenarios.

Execute a Batch File Only on A Specific Day

Have you ever wanted to run a batch file on a specific day, week, month, hour or minute? If so, you can do this with the Windows command line using the %date% or %time% system (environment) variables. To see the contents of these variables, execute the ECHO %time% and ECHO %date% commands at the Windows command line. The following screen shows the data stored in %date% and %time%.

ECHO DATA and TIME Variable

The ~ operator can be used to select a substring from such a variable. Notice that the day of the month is the 8th and 9th characters of the %date% contents at all times. Therefore you can go to the 7th character and then grab the next two and know that you’ve always gotten the day of the month (with days 1-9 represented as 01, 02, 03 …, 09).

With this information, considering the following batch file that uses the IF command and takes advantage of the ~ operator within the %date% variable:

IF %date:~7,2%==13 (GOTO :run_it) else (GOTO :no_run)

:run_it
CLS
REM run whatever command
ECHO Running the command...
DIR C:\
GOTO END

:no_run
REM Notify user that it is the wrong day
ECHO Wrong day!

:END

If this batch file is executed on a 13th day of any month, the DIR C:\ command will be executed. Otherwise, the notification of “Wrong day!” will be displayed for the user. From this example, you should be able to work out methods for determining the day of the month, month of the year and even hour of the day or minute of the hour.

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


 

Pubprn.vbs

The pubprn.vbs command can be used to publish a printer to the Active Directory Domain Services. Similar to prncnfg.vbs and prnmngr.vbs, the Pubprn.vbs command is a Visual Basic script located in the %WINDIR%System32Printing_Admin_Scripts<language> directory. To use this command, at a command prompt, type cscript followed by the full path to the Pubprn.vbs file, or change directories to the appropriate folder.

Syntax (view syntax formatting guide):

Cscript Pubprn.vbs {<ServerName> | <UNCPrinterPath>} "LDAP://CN=<Container>,DC=<Container>"

Syntax Explanation

  • ServerName: Specifies the name of the Windows server that hosts the printer that you want to publish. If you do not specify a computer, the local computer is used.
  • UNCPrinterPath: The Universal Naming Convention (UNC) path to the shared printer that you want to publish.
  • “LDAP://CN=Container,DC=Container“: Specifies the path to the container in Active Directory Domain Services where you want to publish the printer.
  • /?: Displays help at the command prompt.

Explanation with Examples

To publish all printers on the \Server1 computer to the MyContainer container in the MyDomain.Company.Com domain:

cscript Ppubprn.vbs Server1 “LDAP://CN=MyContainer,DC=MyDomain,DC=Company,DC=Com”

To publish the LaserPrinter1 printer on the \Server1 server to the MyContainer container in the MyDomain.Company.Com domain:

cscript Ppubprn.vbs \Server1LaserPrinter1 “LDAP://CN=MyContainer,DC=MyDomain,DC=Company,DC=Com”

 

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

 

Prnqctl.vbs

The Prnqctl.vbs command is used to print a test pages, pause or resume a printer, and clear the printer queue. Similar to the prncnfg.vbs, prndrvr.vbs, prnjobs.vbs, prnmngr.vbs and prnport.vbs commands, the Prnqctl.vbs command is a Visual Basic script located in the %WINDIR%System32Printing_Admin_Scripts<language> directory. To use this command, at a command prompt, type Cscript followed by the full path to the Prnqctl.vbs file, or change directories to the appropriate folder.

Syntax (view syntax formatting guide):

Cscript Prnqctl {-z | -m | -e | -x | -?} [-s <ServerName>] [-p <PrinterName>] [-u <UserName>] [-w <Password>]

Syntax Explanation

  • -z: Pauses printing on the printer specified with the -p parameter.
  • -m: Resumes printing on the printer specified with the -p parameter.
  • -e: Prints a test page on the printer specified with the -p parameter.
  • -x: Cancels all print jobs on the printer specified with the -p parameter.
  • -s ServerName: Specifies the name of the remote computer that hosts the printer that you want to manage. If you do not specify a computer, the local computer is used.
  • -p PrinterName: Specifies the name of the printer that you want to manage. Required.
  • -u UserName -w Password: Specifies an account with permissions to connect to the computer that hosts the printer that you want to manage. All members of the target computer’s local Administrators group have these permissions, but the permissions can also be granted to other users. If you do not specify an account, you must be logged on under an account with these permissions for the command to work.
  • /?: Displays help at the command prompt.

Explanation with Examples

To print a test page on the LaserPrinter1 printer shared by the \Server1 computer:

cscript Prnqctl -e -s Server1 -p LaserPrinter1

To pause printing on the LaserPrinter1 printer on the local computer:

cscript Prnqctl -z -p LaserPrinter1

To cancel all print jobs on the LaserPrinter1 printer on the local computer:

cscript Prnqctl -x -p LaserPrinter1

 

Prnport.vbs

The Prnport.vbs command is used to create, delete, and list standard TCP/IP printer ports, in addition to displaying and changing port configuration. Similar to the prncnfg.vbs, prndrvr.vbs, prnjobs.vbs and prnmngr.vbs, the Prnport.vbs command is a Visual Basic script located in the %WINDIR%System32Printing_Admin_Scripts<language> directory. To use this command, at a command prompt, type cscript followed by the full path to the Prnport.vbs file, or change directories to the appropriate folder.

Syntax (view syntax formatting guide):

Cscript prnport {-a | -d | -l | -g | -t | -?} [-r <PortName>] [-s <ServerName>] [-u <UserName>] [-w <Password>] [-o {raw | lpr}] [-h <HostAddress>] [-q <QueueName>] [-n <PortNumber>] -m{e | d} [-i <SNMPIndex>] [-y <CommunityName>] -2{e | -d}

Syntax Explanation

  • -a: Creates a standard TCP/IP printer port.
  • -d: Deletes a standard TCP/IP printer port.
  • -l: Lists all standard TCP/IP printer ports on the computer specified with the -s parameter.
  • -g: Displays the configuration of a standard TCP/IP printer port.
  • -t: Configures the port settings for a standard TCP/IP printer port.
  • -r PortName: Specifies the port to which the printer is connected.
  • -s ServerName: Specifies the name of the remote computer that hosts the printer that you want to manage. If you do not specify a computer, the local computer is used.
  • -u UserName -w Password: Specifies an account with permissions to connect to the computer that hosts the printer that you want to manage. All members of the target computer’s local Administrators group have these permissions, but the permissions can also be granted to other users. If you do not specify an account, you must be logged on under an account with these permissions for the command to work.
  • -o {raw | lpr}: Specifies which protocol the port uses: TCP raw or TCP LPR. If you use TCP raw, you can optionally specify the port number by using the -n parameter. The default port number is 9100.
  • -h HostAddress: Specifies (by IP address) the printer for which you want to configure the port.
  • -q QueueName: Specifies the queue name for a TCP raw port.
  • -n PortNumber: Specifies the port number for a TCP raw port. The default port number is 9100.
  • -m{e | d}: Specifies whether SNMP is enabled. The parameter e enables SNMP. The parameter d disables SNMP.
  • -i SNMPIndex: Specifies the SNMP index, if SNMP is enabled. For more information, see RFC 1759 at the RFC Editor Web site (http://go.microsoft.com/fwlink/?LinkId=569).
  • -y CommunityName: Specifies the SNMP community name, if SNMP is enabled.
  • -2{e | -d}: Specifies whether double spools (also known as respooling) are enabled for TCP LPR ports. Double spools are necessary because TCP LPR must include an accurate byte count in the control file that is sent to the printer, but the protocol cannot get the count from the local print provider. Therefore, when a file is spooled to a TCP LPR print queue, it is also spooled as a temporary file in the system32 directory. TCP LPR determines the size of the temporary file and sends the size to the server running LPD. The parameter e enables double spools. The parameter d disables double spools.
  • /?: Displays help at the command prompt.

Explanation with Examples

To display all standard TCP/IP printing ports on the server \Server1:

cscript Prnport.vbs -l -s Server1

To delete the standard TCP/IP printing port on the server \Server1 that connects to a network printer at 10.2.3.4:

cscript Prnport.vbs -d -s Server1 -r IP_10.2.3.4

To add a standard TCP/IP printing port on the server \Server1 that connects to a network printer at 10.2.3.4 and uses the TCP raw protocol on port 9100:

cscript Prnport.vbs -a -s Server1 -r IP_10.2.3.4 -h 10.2.3.4 -o raw -n 9100

To enable SNMP, specify the “public” community name and set the SNMP index to 1 on a network printer at 10.2.3.4 shared by the server \Server1:

cscript Prnport.vbs -t -s Server1 -r IP_10.2.3.4 -me -y public -i 1 -n 9100

To add a standard TCP/IP printing port on the local computer that connects to a network printer at 10.2.3.4 and automatically get the device settings from the printer:

cscript Prnport.vbs -a -r IP_10.2.3.4 -h 10.2.3.4

 

Prnmngr.vbs

The Prnmngr.vbs command is used to add, delete, and list printers or printer connections. The prnmngr.vbs command can also be used to set and display the default printer. Similar to the prncnfg.vbs, prndrvr.vbs and prnjobs.vbs commands, the prndrvr.vbs command is a Visual Basic script located in the %WINDIR%System32Printing_Admin_Scripts<language> directory. To use this command, at a command prompt, type cscript followed by the full path to the Prnmngr.vbs file, or change directories to the appropriate folder

Syntax (view syntax formatting guide):

Cscript Prnmngr {-a | -d | -x | -g | -t | -l | -?}[c] [-s <ServerName>] [-p <PrinterName>] [-m <PrinterModel>] [-r <PortName>] [-u <UserName>] [-w <Password>]

Syntax Explanation

  • -a: Adds a local printer connection.
  • -d: Deletes a printer connection.
  • -x: Deletes all printers from the server specified with the -s parameter. If you do not specify a server, Windows deletes all printers on the local computer.
  • -g: Displays the default printer.
  • -t: Sets the default printer to the printer specified by the -p parameter.
  • -l: Lists all printers installed on the server specified by the -s parameter. If you do not specify a server, Windows lists the printers installed on the local computer.
  • c: Specifies that the parameter applies to printer connections. Can be used with the -a and -x parameters.
  • -s ServerName: Specifies the name of the remote computer that hosts the printer that you want to manage. If you do not specify a computer, the local computer is used.
  • -p PrinterName: Specifies the name of the printer that you want to manage.
  • -m DriverModelName: Specifies (by name) the driver you want to install. Drivers are often named for the model of printer they support. See the printer documentation for more information.
  • -r PortName: Specifies the port where the printer is connected. If this is a parallel or a serial port, use the ID of the port (for example, LPT1: or COM1:). If this is a TCP/IP port, use the port name that was specified when the port was added.
  • -u UserName -w Password: Specifies an account with permissions to connect to the computer that hosts the printer that you want to manage. All members of the target computer’s local Administrators group have these permissions, but the permissions can also be granted to other users. If you do not specify an account, you must be logged on under an account with these permissions for the command to work.
  • /?: Displays help at the command prompt.

Explanation with Examples

To add a printer named ColorPrinter_2 that is connected to LPT1 on the local computer and requires a printer driver called Color Printer Driver1:

cscript Prnmngr.vbs -a -p ColorPrinter_2 -m “Color Printer Driver1” -r lpt1:

To delete the printer named ColorPrinter_2 from the remote computer named HRServer:

cscript Prnmngr.vbs -d -s HRServer -p ColorPrinter_2

 

Prnjobs.vbs

The Prnjobs.vbs command is used to pause, resume, cancel and list the print jobs in a specified print queue. Similar to the prncnfg.vbs and prndrvr.vbs command, the prnjobs.vbs command is a Visual Basic script located in the %WINDIR%System32Printing_Admin_Scripts<language> directory. To use the prnjobs.vbs command at the command prompts, type Cscript followed by the full path to the Prnjobs.vbs file, or change directories to the appropriate folder.

Syntax (view syntax formatting guide):

Cscript Prnjobs {-z | -m | -x | -l | -?} [-s <ServerName>]
[-p <PrinterName>] [-j <JobID>] [-u <UserName>] [-w <Password>]

Syntax Explanation

  • -z: Pauses the print job specified with the -j parameter.
  • -m: Resumes the print job specified with the -j parameter.
  • -x: Cancels the print job specified with the -j parameter.
  • -l: Lists all the print jobs in a print queue.
  • -s ServerName: Specifies the name of the remote computer that hosts the printer that you want to manage. If you do not specify a computer, the local computer is used.
  • -p PrinterName: Specifies the name of the printer that you want to manage. Required.
  • -j JobID: Specifies (by ID number) the print job you want to cancel.
  • -u UserName -w Password: Specifies an account with permissions to connect to the computer that hosts the printer that you want to manage. All members of the target computer’s local Administrators group have these permissions, but the permissions can also be granted to other users. If you do not specify an account, you must be logged on under an account with these permissions for the command to work.
  • /?: Displays help at the command prompt.

Explanation with Examples

To pause a print job with a job ID of 27 sent to the remote computer named HRServer for printing on the printer named ColorPrinter, type:

Cscript Prnjobs.vbs -z -s HRServer -p ColorPrinter -j 27

To list all current print jobs in the queue for the local printer named ColorPrinter_2, type:

Cscript Prnjobs.vbs -l -p ColorPrinter_2