Solutions providers takeaway: Use this chapter excerpt to become an expert on Windows 7 command-line tools and programs that can help you maintain, configure and diagnose tasks on Windows 7.
By submitting your email address, you agree to receive emails regarding relevant topic offers from TechTarget and its partners. You can withdraw your consent at any time. Contact TechTarget at 275 Grove Street, Newton, MA.
Despite the ease of use of the Windows graphical user interface, the command-line interface remains a useful way to perform many maintenance, configuration, and diagnostic tasks. Many of the most important diagnostic tools such as ping, tracert, and nslookup are available only from the command line, unless you purchase third-party graphical add-ons to perform these functions. And although the term "batch file" might bring back uncomfortable memories of the old MS-DOS days, batch files and program scripts are still powerful tools that provide a useful way to encapsulate common management functions. Together, command-line utilities, batch files, and scripts based on Windows Script Host provide a complete set of building blocks from which you can build high-level utilities for repetitive or complex tasks.
In this book, I only have room to show you how to set up and configure the command-line environment. For much more detail, tutorials, examples, and many helpful tips on using command-line tools, check out Brian's book Windows 7 and Vista Guide to Scripting, Automation, and Command Line Tools, published by Que.
The Windows 7 Command Prompt Environment
To open a Command Prompt window in which you can type commands and review output, as shown in Figure 29.1, click Start, All Programs, Accessories, Command Prompt. Alternately, click Start and type cmd in the Search box. Then, when cmd.exe has been located, press Enter.
The Command Prompt window is the gateway to a world of powerful Windows management tools.
The main difference between a standard Windows application and a command-line program -- which in Windows is technically called a console program -- is that it doesn't use a graphical display or pull-down menus. Instead, you type commands into the Command Prompt window to tell Windows to do something, and the programs type information back to you. Each command line starts with the name of the program that you want to run, followed by additional information called arguments. Arguments tell the program what specifically you want to do.
If you plan on using the Command Prompt window regularly, pin it to the taskbar for quick access. Right-click the Command Prompt window icon in the taskbar and select Pin This Program to Taskbar.
When you type a command line, Windows searches a list of folders called the search path for a file whose name starts with the program name you typed and whose name ends with any of several extensions such as .exe, .bat, and .vbs. The most common program extensions are listed in Table 29.1. Windows examines the file to see what type of program it is, and then runs it. It's then the program's job to interpret any arguments you typed after the program name.
You can also open a Command Prompt window from Windows Explorer. Hold the Shift key down, right-click a folder name, and select Open Command Window Here. The window will open with the selected folder as the default folder.
Table 29.1 Typical Executable Program Extensions
|.bat, .cmd||Batch file|
|.com||Archaic MS-DOS program|
|.exe||Windows GUI program, console program, or MS-DOS program (Windows determines which by examining the contents of the file)|
|.msc||Microsoft Management Console snap-in|
|.vbs||Script file written in the VBScript language|
The search path is defined by a list of folder names in the PATH environment variable, and the complete list of extensions that Windows looks for is defined in the PATHEXT environment variable. The default search path includes the following folders:
This means that any program file, batch file, or script stored in any of these folders can be run simply by typing its name. You can start both Windows programs and command-line programs in this way. For example, you just have to type notepad to start the Notepad accessory.
If you create your own batch files, scripts, or programs, it's a good idea to create a separate folder to store them in, and to put that folder in the search path. I'll show you how to do so later in this chapter, under "Setting the PATH Environment Variable."
Running Commands with Elevated Privileges
Some command-line programs require elevated privileges (via User Account Control) to do their job correctly. To run a command-line program with elevated privileges, you must run it from a Command Prompt window that is itself "elevated."
Be very careful when using an elevated Command Prompt window. Any commands you start from within this window will run with elevated privileges from the get-go, and you will receive no further UAC prompts when you start them. This includes Windows GUI programs -- for example, if you type the command optionalfeatures, you will get the Turn Windows Features On or Off dialog box, and you will not have to confirm anything before it starts.
To be safe, do not use an elevated Command Prompt window for general-purpose work. Use it only to accomplish a specific task that requires elevated privileges; then close it.
To open an elevated Command Prompt window, click Start, All Programs, Accessories. Then right-click Command Prompt and select Run As Administrator. Or, if you have the Command Prompt icon pinned to your taskbar, there are two quick ways to open an elevated prompt:
- Right-click the icon, right-click the Command Prompt label in the Jump List pop-up, and then select Run As Administrator.
- Hold down the Shift+Ctrl keys and click the icon.
If you want, you can set a Command Prompt shortcut or pinned taskbar icon so that it is elevated by default. Right-click the icon and select Properties. On the Shortcut tab, click the Advanced button, and check Run As Administrator. Be sure to rename the shortcut so that it's clear that it opens an elevated prompt.
Learning About Command-Line Programs
How do you know what programs are available and how to use them? For that, you have to turn to documentation about the command-line environment. For some reason, Microsoft no longer provides this in the Help and Support system, but you can search online, and some programs can be told to display their own usage information. To show you what's available, we have posted a list of all the programs included with Windows 7 at www.helpwin7.com. And you might want to check out Brian's book Windows 7 and Vista Guide to Scripting, Automation, and Command Line Tools. For a general online listing, perform this Google search, and then locate the A--Z listing for Windows Server 2008 or Windows Server 2003. Most of the programs listed there are available on Windows 7.
- site:microsoft.com command line a-z windows server
- To get additional information on a command that interests you, try the following sources, in the order listed. I'll use the rasdial command in the examples that follow, but you can use the same technique with any command that interests you.
- A majority of command-line commands will print help information if you add /? to the command line. For example, to get information for the rasdial command, type rasdial /?.
- If the command prints so much text that it scrolls out of view, use one of the following techniques to read it all:
- Use the Command Prompt window's scrollbars to back up.
- Press F3 to recall the command line, add | more to the end of the line, and press Enter. This will "pipe" the help listing through the more command, which displays it one screenful at a time. Press Enter after reading each screen.
- Type the command help rasdial. If too much text prints, use the techniques just listed to manage the overflow.
- Open Internet Explorer and type rasdial in the Search window. You might also try the Google search site:microsoft.com rasdial.
Not every one of those information sources will work for every command, but at least one should lead you to an explanation of what the command does and what its command-line options are, and provide some examples of its use. The command-line options for Windows 7, Vista, XP, and Server 2003 and Server 2008 are pretty much the same, so if you can't find any Windows 7--specific information, documentation for the other versions should be okay to use.
Cutting and Pasting in the Command Prompt Window
Although you will usually use output redirection to store the output from command-line programs in files, you can also use cut and paste to move text into or out of a Command Prompt window.
To paste text into the window at the cursor location, click the window's System Menu (the upper-left corner) and select Edit, Paste. It's easier to do this without the mouse: just press Alt+Spacebar and type E P.
To copy text from the window to the Clipboard, click the window's System Menu and select Edit, Mark. Alternatively, type Alt+Spacebar E M. Use the mouse to highlight a rectangular area of the screen, and then press Enter. This copies the text to the Clipboard.
By default, the mouse does not select text until you use the Mark sequence. This makes it easier to use MS-DOS programs that are mouse-aware. If you seldom use the mouse with MS-DOS applications, click the System Menu (or press Alt+Spacebar), select Defaults, and check Quick Edit. When Quick Edit is enabled, you can use the mouse to mark text for copying to the Clipboard without having to type Alt+Spacebar E M first.
Printed with permission from Que Publishing. Copyright 2009. Windows 7 In Depth by Robert Cowart and Brian Knittel. For more information about this title and other similar books, please visit Que Publishing.