Scripting‎ > ‎

Command Prompt Tricks

Back before the invention of the mouse and Graphical User Interface, there was the command prompt.  Every operating system (e.g. UNIX, CP/M, MS-DOS, VMS, etc.) of the day had some type of command line interface.  If you're a command prompt power user or purest, the tricks below will show you how to fully utilize the Window's command prompt. 

This document is not meant as a introduction to the command line, its meant to help command prompt power users get the most out of this feature.



Copy Text From Command Prompt

Right-click on the title bar and select Edit and Mark. Left-click and select the text that you want to copy into the clipboard, then press the Enter key.   Right-click to paste the text into the command prompt, or choose the Paste command (control-v) to paste the text into a Windows application.

Quick Edit Mode

Right-click on the title bar and click Properties. Under the Options tab check the box for QuickEdit Mode.  Press the OK button when done.

After that you can left-click and select text that you want to copy into the clipboard, then press the Enter key. Right-click to paste the text into to the command prompt, or choose the Paste command (control-v) to paste the text into a Windows application.

Customizing the Command Prompt Look

All customization options are available by right-clicking on the title bar and clicking Properties. From here you can change the cursor size, window size, fonts, colors and more.  It's highly recommended that you click on the Layout tab,  in the Screen Buffer Size section set it to 9999.  This will allow you to go back through your command prompt history to later review or save it.


Environmental Variables

At the command prompt type SET to display all the environmental variables on the local system.  These environmental variables are used for several things, for example the %Path% (note '%VariableName%' between the percentage signs is used to designate these items) this variable holds the search path that Windows will use to try to find a command that is entered.  Instead of typing C:\Users\Tim\Desktop to get to your desktop, you could type %UserProfile%\Desktop.

Wildcard Characters

Windows supports two wildcard characters, which are the '*' (asterisk sign) and '?' (question mark).  The '*' wildcard can represent any number of alphanumeric characters, for example: DIR C:\Windows\System32\*.EXE (this would display all files that have the .EXE extension).  The '*' wildcard can represent signal alphanumeric characters, for example: DIR C:\Windows\System32\Example.D?? (this would display all files that have the name of example, and an extension that begins with "D").

Starting the Command Prompt in Elevated Mode

To open the command prompt with administrative elevated privileges, from the Start Menu, in the Search field (or from the Run... command) type CMD then press Ctrl+Shift+Enter.

"Open Command Window Here"

To open a command prompt referenced to a folder of your choice. Hold down the Shift key while right-clicking a folder, in the context menu select Open Command Window Here.

Running Multiple Commands

  • You can run multiple commands in series by separating them with comand1 & comand2
    • Example: dir z: & dir c: (note Z: can be any drive that doesn't exist)
    • Note: The commands are not run simultaneously, after the command completes (successfully or not) then the second command will run.
  • You can run multiple commands in series by separating them with comand1 && comand2
    • Example: dir z: && dir c: (note Z: can be any drive that doesn't exist)
    • Note: The commands are not run simultaneously, after the command completes successfully then the second command will run. If the first command fails, then the second command will not run
  • You can run secondary command in series if the first command fails by separating them with comand1 || comand2
    • Runs the second command only if the first command fails (e.g, receives an error code greater than zero).

Change the Command Console:
  • Changes the size using the mode command: mode 80,50 or mode con: cols=80 lines=50
  • Change the title of the console window using: title This is a test...
  • Change the command prompt: prompt $t - $d $p$_$G (for more information, type prompt /?)
  • Change the color of the command prompt: color f0

Windows Sleep Command

When creating a batch file you sometimes need to script to pause for a few seconds before executing the next command.  Unfortunately Windows batch doesn't support a build-in sleep command.  Although, you can work around this by using (forces the script to pause for roughly 5 seconds):
  • ping -n 5 127.0.0.1 > NUL 2>&1
  • choice /T 5 /D y > nul
  • timeout /T 5

Redirectors

Command prompt redirectors allow you to manipulate how the input or output of a program is displayed or used. Redirectors are used by appending them to the end of a command followed by what you are redirecting to.  For example: dir > dir.txt (send the output form the DIR command sends it to a text file). 

There are four command prompt redirectors:
  • command1 > filename.ext
    • This redirector will take the output of a program and store it in a file.  If the file exists, it will be overwritten.  If it does not exist it will create a new file.  For example the command dir > dir.txt will take the output of the dir command and place it in the dir.txt file.  If dir.txt exists, it will overwrite it, otherwise it will create it.
  • command1 >> filename.ext
    • This redirector will take the output of a program and store it in a file.  If the file exists, the new data will be appended to the current data in the file rather than overwriting it.  If it does not exist it will create a new file.  For example the command dir >> dir.txt will take the output of the dir command and appends it to the existing data in the dir.txt file if the file exists.  If dir.txt does not exist, it will create the file first.
  • command1 < filename.ext
    • This redirector will take the input for a program from a specified file.  For example the date command expects input from a user.  So if we had the command date < date.txt, it would take the input for the date program from the information contained in the date.txt file.
  • command1 | command2
    • This redirector is called a pipe.  It will take the output of a program and pipe it into another program.  For example dir /b | sort would take the output of the dir command and use it as input to the sort command.
  • command1 2> filename.ext
    • This redirector will take the error output of a program and store it in a file. For example, if you typed "dir x:" and X: drive didn't exist on your system, you would see the following error "The system cannot find the path specified."  If you typed "dir x: 2> test.txt" the following error "The system cannot find the path specified." would be sent to a text file called TEST.TXT.
Note: The command prompt supports devices like AUX, COMn, LPTn, NUL and PRN and treats them like files (these are known as 'reserved names').  Some of these device names are obsolete if your computer doesn't have these ports on them (such as: COMn and LPTn).

Redirecting Output from the Command Prompt to the Windows Clipboard
  • The CLIP.EXE command-line utility is used to redirect output from a command to the Windows clipboard.
    • For example:
      • Send a directory listing to the clipboard: dir | clip
      • Send a text file into the clipboard: textfile.txt > clip

Command Prompt Keyboard Shortcuts

  • F1: Paste the previous command one character at a time
  • F2: Copies a certain number of characters from the previous command to the current one.
    • For example, type nslookup example.com, press enter.
    • Press F2, then press . (period), nslookup example. will be displayed.
  • F3: Paste the previous command.
  • F4: Deletes a certain number of characters from the previous command to the current one.
    • For example, type nslookup example.com, press enter.
    • Press the up arrow, to display the previous command (e.g. nslookup example.com)
    • then move the cursor to E in example, press F4 then the . (period), nslookup .com will be displayed.
  • F5: Pastes recently executed commands
  • F6: Prints EOF character (Ctrl+Z)
  • F7: Display command history, and allows you to select and run it
  • F8: Cycles through command history
  • F9: Enter number of the previous command that you want to run
  • Tab: Auto-completes file and folder path names
  • Up and Down Arrows: Previous commands
  • ESC: Erases the current command at the command prompt.
 

Comments