COMMAND-LINE and AUTOMATION TOOLS Command-Line Tools

COMMAND-LINE and AUTOMATION TOOLS Command-Line Tools

29 COMMAND-LINE AND AUTOMATION TOOLS Command-Line Tools Despite the ease of use of the Windows graphical user interface, the command-line interface remains a useful way to perform many mainte- nance, 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 tip scripts are still powerful tools In this book, I only have room to that provide a useful way to show you how to set up and config- encapsulate common manage- ure the command-line environment. ment functions. Together, com- For much more detail, tutorials, mand-line utilities, batch files, examples, and many helpful tips on and scripts based on Windows using command-line tools, check out Script Host provide a complete Brian’s book Windows 7 and Vista set of building blocks from Guide to Scripting, Automation, and which you can build high-level Command Line Tools, published by Que. utilities for repetitive or com- plex tasks. 822 Command-Line and Automation Tools ART P VI 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. Figure 29.1 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 techni- tip cally called a console program—is that it doesn’t use a graphi- If you plan on using the Command cal display or pull-down menus. Instead, you type commands Prompt window regularly, pin it to into the Command Prompt window to tell Windows to do the taskbar for quick access. Right- something, and the programs type information back to you. click the Command Prompt window Each command line starts with the name of the program that icon in the taskbar and select Pin you want to run, followed by additional information called This Program to Taskbar. arguments. Arguments tell the program what specifically you want to do. When you type a command line, Windows searches a list of folders called the search path for a file whose name starts tip with the program name you typed and whose name ends with You can also open a Command any of several extensions such as .exe, .bat, and .vbs. The Prompt window from Windows most common program extensions are listed in Table 29.1. Explorer. Hold the Shift key down, right-click a folder name, and select Windows examines the file to see what type of program it is, Open Command Window Here. The and then runs it. It’s then the program’s job to interpret any window will open with the selected arguments you typed after the program name. folder as the default folder. C HAPTER The Windows 7 Command Prompt Environment 823 29 Table 29.1 Typical Executable Program Extensions Extension Program Type .bat, .cmd Batch file .com Archaic MS-DOS program .exe Windows GUI program, console program, or MS-DOS program (Windows deter- mines which by examining the contents of the file) .js Script file written in the JavaScript language .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 com- plete list of extensions that Windows looks for is defined in the PATHEXT environment variable. The default search path includes the following folders: C:\Windows\system32 C:\Windows C:\Windows\System32\Wbem C:\Windows\System32\WindowsPowerShell\v1.0 This means that any program file, batch file, or script stored in any of these folders can be run sim- ply 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.” caution Running Commands with Elevated Be very careful when using an ele- Privileges vated Command Prompt window. Any commands you start from within Some command-line programs require elevated privileges (via this window will run with elevated User Account Control) to do their job correctly. To run a com- privileges from the get-go, and you mand-line program with elevated privileges, you must run it will receive no further UAC prompts from a Command Prompt window that is itself “elevated.” when you start them. This includes To open an elevated Command Prompt window, click Start, All Windows GUI programs—for exam- Programs, Accessories. Then right-click Command Prompt and ple, if you type the command optionalfeatures, you will get the select Run As Administrator. Or, if you have the Command Turn Windows Features On or Off Prompt icon pinned to your taskbar, there are two quick ways to dialog box, and you will not have to open an elevated prompt: confirm anything before it starts. • Right-click the icon, right-click the Command Prompt label in To be safe, do not use an elevated the Jump List pop-up, and then select Run As Administrator. Command Prompt window for general- purpose work. Use it only to accom- • Hold down the Shift+Ctrl keys and click the icon. plish a specific task that requires elevated privileges; then close it. 824 Command-Line and Automation Tools ART P VI 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 but- ton, 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 pro- vides 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 tech- niques 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 infor- mation, documentation for the other versions should be okay to use. C HAPTER Setting Environment Variables 825 29 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.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    21 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us