There are several commands that you should become very familiar with...these are some of the command-line tools that you'll use nearly every day once you've become a master of command line-fu. Note There are, of course, hundreds of others. Appendix B, " Windows Command Reference," contains a listing of all standard Windows programs, and the ones marked CMD are command-line programs. You might want to scan through that listing to see the range of programs that is available. For some tips about getting usage instructions for programs that interest you, see "Interpreting Command Line Syntax" earlier in this chapter. And Appendix A lists hundreds more add-on tools that you can obtain. cd Each Command Prompt window has the concept of a current drive and current directory (also called the default directory ), which is its starting place when looking for files. Although Windows Explorer displays its current directory in its status and address bars, it's most common for the Command Prompt window to show the current directory name in its prompt; the indicator it prints to tell you it's ready to accept another command. For example, the prompt C:\Documents and Settings\brian> indicates that the current drive is C: and the current directory is \Documents and Settings\brian . You can change the current drive by typing a command line that consists only of a drive letter and colon , for example d: You can change the current directory by typing the cd or equivalently the chdir command, followed by a path name: cd \documents and settings\brian Note For the cd command (only), quotes around a pathname that contains spaces are optional. Each drive letter has its own default directory, so the commands cd c:\program files cd d:\setup cd e:\temp set the default directory on c: , d: , and e: drives without changing the default drive letter. You can specify full or relative paths. If the path doesn't start with a \ character, it's interpreted relative to the current directory. For example, if the current directory is \Documents and Settings , the commands cd \Documents and Settings\brian and cd brian are equivalent. The command cd \ returns to you the current drive's root (top level) directory. The special directory name .. is interpreted as "the directory above," or the parent directory, so if the current directory is C:\Documents and Settings\brian , the command cd .. changes to C:\Documents and Settings , and cd ..\scott changes to C:\Documents and Settings\scott . You can make the cd command change the drive and directory by adding /D to the command line, as in cd /D d:\setup\english This is especially useful in batch files, where you might want to change to a specific drive and directory whose name is specified in an environment variable, as in cd /d %userprofile%\My Documents which changes to your My Documents folder no matter the drive on which it's located. pushd and popd The command pushd path changes the current directory to the specified path . The previous current drive and directory are remembered , and the command popd restores the previous path. Pushd saves as many directory changes as you care to make, and each popd returns to the directory in use before the corresponding pushd . If the path specified to pushd contains a drive letter, the current drive is changed as well. When command extensions are enabled (which they are, by default), you can specify a network path, for example, \\server\sharename\path , and pushd will automatically map a drive letter to the network path, starting with the letter Z and working backwards . Popd automatically deletes the temporary drive mappings. Note If you search your hard drive for cd.exe , pushd.exe , or popd.exe , you'll find that they don't exist. There are no executable files corresponding to these commands. They are built-in commands handled directly by cmd.exe . dir The dir command lists the contents of directories (folders). It's handy enough to know that the command dir by itself lists the contents of the current directory; with this and cd , you can explore the entire hard disk. Dir has dozens of options. I won't list them all here; you can see the entire list by typing dir /? at the command prompt. Here are a few of the most useful variations: Command | Description | dir /p | Prints a listing, pausing when the screen fills with text. Press Enter to continue the listing. You can use /p with any of the command variations. | dir filename | Lists just files matching filename . You can use wildcard characters in the filename : * matches anything; ? matches exactly one character. For example, dir *.exe lists all files ending with .exe , and dir print*.* lists all files whose names start with print . Put the filename in quotes if the name contains spaces. You can also specify a drive letter and/or path to search a particular drive or directory. | dir ... /od | Sorts the files from youngest to oldest. | dir ... /oen | Lists files sorted by extension ( .xxx ), then by name. | dir ... /ah | Lists hidden files. | dir ... /s | Lists the current or specified directory and its subdirectories as well; for example, dir /s "c:\program files\*.exe " lists all .EXE files in c:\program files and all of its subdirectories. | dir ... /b | Removes the file date, size and other information. Prints just the names of matched or located files. | The last variation I've found especially useful when I'm creating a batch file that is to perform some operation on a list of files. I use a command line dir /b *.txt >mybatch.bat which puts all of the names into file mybatch.bat . Then I can edit mybatch.bat and put commands before each of the filenames. more Many command-line programs print out more text than fits in the console window. In most cases, you can resize the window or scroll the window contents up to read it all, but there's another way. The utility more displays text a screen at a time, letting you press a key when you're ready to move on to the next screen. There are two easy ways to use more: You can view a file with the command more filename but most often, you'll use more to page through the output of another program. With no filename argument, more reads the standard input, so you can pipe text to it like this: dir /s more Because the command dir /s lists the contents of the current directory and all subdirectories, its output is often quite long. Piping the output through more pauses it after each screen. When more has halted, it will display -- More -- at the bottom of the screen. Press the space or Enter key to move on to the next screen. There are some command-line options and other functions available as well. To read about the type more /? or rather, more /? more runas runas lets you run a program using the credentials of another user. The program will appear in your command prompt window (or, if it's a GUI program, on your desktop), but it will have the rights and privileges of another user . It's especially handy when you want to quickly run an administrative program as Administrator without logging on using that privileged account. The full syntax of the runas command is runas [ /noprofile ] [ /env ] [ /netonly ] [ /smartcard ] [ /user: username ] command line The command line options are: Command | Description | /noprofile | Specifies that the user's profile (Registry settings) should not be loaded. This causes the application to load more quickly, but can make some applications malfunction. | /env | Uses the current environment variables instead of the user's default variables . | /netonly | The credentials specified are for remote access only, not local file access. | /smartcard | Uses the credentials on a smart card. If this option is used, the /user option may be omitted. | /user | Specifies the user account to use to run the program. Can be a local account (for example, Administrator), or a domain account in the format user@domain or domain\user . | command line | The command to run, and any additional arguments. | I've found that I use runas frequently, but in only three ways: -
runas /user:Administrator mmc opens the Microsoft Management Console. From there, you can click File, Add/Remove Snap In to open management tools. -
runas /user:Administrator control panelname .cpl , where panelname is the name of a Control Panel applet file. I'll talk about this more in the next section. -
runas /user:Adminstrator cmd opens a Command Prompt window with Administrator privileges. The new window will run under the Administrator logon name, and any programs you run from this command prompt will also have Administrator privileges. The only exception to this is that you can't run Explorer from this new window, or any window derived from Explorer, for example, Printers and Faxes. Explorer is an odd program, and you can't have two copies running under two different accounts at the same time. control You can run Control Panel applets from the command line using the control command. There are three reasons you might want do to this: -
To more quickly open a control panel. If you already have a command prompt window open, it's faster to type control firewall.cpl than to poke your way through the Start menu, open the Control Panel, and then open the Windows Firewall window. -
To run a control panel applet as a Computer Administrator. Again, it's much faster to type runas /user:Administrator "control firewall.cpl" than to log off and back on, or to switch users. -
To run a control panel applet that is not listed in the Control Panel window. For example, to configure Windows to log on automatically at startup, you have to type control userpasswords2 , or if you're not logged on as a Computer Administrator, runas /user:Administrator "control userpasswords2" The syntax for the control command has three variations: control filename .cpl | Opens the primary control panel applet contained in the file filename .cpl stored in \windows\system32 or elsewhere in the PATH . | control filename .cpl appletname | Opens an alternate applet contained in a .CPL file; some .CPL files contain code for more than one control panel applet. | control specialname | Opens a control panel applet or system configuration window corresponding to a special name recognized by control.exe , or listed in the Registry under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Control Panel\Cpls or HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Control Panel\Cpls . | Table 9.10 lists the various control panel applets and system windows you can open from the command line. Not all versions or installations of Windows will have all these control panels. Table 9.10. Control Panels Applets from the Command Line Argument(s) after control | Applet Title | access.cpl | Accessibility Options | admintools | Administrative Tools [*] | appwiz.cpl | Add/Remove Programs | bthprops.cpl | Bluetooth Properties | color | Display Properties, Appearance tab | cttune.cpl | ClearType Tuning (source: PowerToys for Windows XP download) | date/time | Date and Time Properties | desk.cpl | Display Properties, Themes tab | desktop | Display Properties, Themes tab | fax.cpl | Fax Properties (Windows 2000) | firewall.cpl | Windows Firewall | folders | Folder Options | fonts | Fonts [*] | hdwwiz.cpl | Add Hardware | inetcpl.cpl | Internet Options | infrared | Wireless Connection (IRDA infrared) | international | Regional and Language Options | intl.cpl | Regional and Language Options | irprops.cpl | Wireless Link (IRDA infrared) | joy.cpl | Game Controllers | main.cpl mouse | Mouse Properties | main.cpl keyboard | Keyboard Properties | main.cpl pc card (PCMCIA) | PCMCIA or Removable Hardware | main.cpl power | Power Management (Windows 95 only) | mlcfg32.cpl | Windows Messaging profile manager (Windows 98/Me only) | mmsys.cpl | Sounds and Audio Devices | modem.cpl | Modem properties (Windows 98/Me only) | mouse | Mouse Properties | msmq.cpl | Microsoft Message Queuing Service | ncpa.cpl | Network Connections, but much slower to open than control netconnections [*] | netconnections | Network Connections [*] | netcpl.cpl | Network Configuration (Windows 98/Me only) | netsetup.cpl | Network Setup Wizard | NetWare | Client Services for NetWare | nusrmgr.cpl | User Accounts | nwc.cpl | Client Services for NetWare | odbccp32.cpl | ODBC Data Source Administrator | password.cpl | Change Passwords/Remote Administration/User Profiles (Windows 98/Me only) | ports | System Properties, Computer Name tab | powercfg.cpl | Power Options | printers | Printers and Faxes [*] | sapi.cpl | Speech Properties | scannercamera | Scanners and Cameras [*] | schedtasks | Scheduled Tasks [*] | speech | Speech Properties | sticpl.cpl | Scanners and Cameras | sysdm.cpl | System Properties | sysdm.cpl add new hardware | Add New Hardware wizard | telephon.cpl | Phone and Modem options | telephony | Phone and Modem options | themes.cpl | Desktop themes (Windows 98/Me only) | timedate.cpl | Date and Time Properties | tweakui.cpl | Tweak UI (Windows 98, ME, and 2000 version only; Windows XP version is a Start menu item) | ups.cpl | UPS properties or Power Management | userpasswords | User Accounts | userpasswords2 | On Windows XP, displays Windows 2000 version of User Accounts. Allows setting of automatic logon account. Prompts for Administrator password if necessary, no need to use runas . | wgpocpl.cpl | Workgroup Postoffice Admin (Windows 98/Me only) | wscui.cpl | Security Center | wuaucpl.cpl | Automatic Updates | [*] This control panel does not work when started by runas To run a control panel as the Computer Administrator, the control command must be enclosed in quotes, and preceded by the runas command, as in runas /user:Administrator "control appwiz.cpl" net The net command is a universe of its own; it performs 22 different functions involving networking and (strangely enough) Windows service management. The net command by itself lists these functions. These are very handy commands to know if you spend a lot of time working with networked computers. The functions available under Windows 2000 and XP are listed in Table 9.11. Table 9.11. net Subcommands net Subcommand | Function | net accounts | Adjusts the password requirements settings for local accounts on this computer | net computer | Adds or deletes computers from a domain network | net config | Displays or sets parameters for the server (file sharing server) or workstation (file sharing client) Microsoft networking components | net continue | Reactivates a paused service | net file | Lists your files that are being used by others, and optionally can disconnect the file and clear its "locked" status | net group | Lists, adds, or modifies global user groups on the local computer or domain | net help | Displays help information for these subcommands | net helpmsg | Prints a text description of a network error code number | net localgroup | Lists, adds, or modifies local user groups on the local computer or domain server | net name | Lists the names or aliases for the Windows Message service (this is not the same as Windows Messenger; it's a rarely used notification service) | net pause | Pauses a Windows service on the local computer | net print | Lists and manages print jobs queued on the local computer or another networked computer | net send | Sends a message to another user or computer (again, this service is rarely used and is likely disabled on Windows XP) | net session | Lists network files in use by the local computer or another networked computer; can also disconnect open files | net share | Lists, adds, or removes shared folders | net start | Starts a Windows service | net statistics | Prints network data transfer statistics | net stop | Stops a Windows service | net time | Synchronizes the computer's clock with another computer or time server | net use | Maps a drive letter to a shared folder, or a printer LPT port to a shared printer | net user | Lists, adds, or manages local user accounts | net view | Lists computers in a network workgroup or domain, and can also list folders and printers shared by a specific computer | You can get help for any of these commands by typing net help subcommand on the command line. The most important uses are listed in the following sections. These are especially useful in batch files that have to access shared folders; the batch file can create drive mappings on demand. net view The net view subcommand has several useful variations: Command | Description | net view | Lists all of the computers in the current domain or workgroup | net view /domain | Lists all of the domains on the network | net view /domain: xxx | Lists all of the computers in the specified domain xxx | net view /network:nw | Lists all available Novell NetWare Servers | net view \\ computername | Lists all of the printers and folders shared by the specific computer | net use The net use subcommand lets you map networked files and folders to drive letters or printer devices on your computer. There are several variations: Command | Description | net use x : \\ computer\sharename | Maps drive letter x : to shared folder sharename on computer named computer. | net use x : \\ computer\sharename\subfolder \ ... | Maps drive letter x : to a subfolder of the shared folder sharename on computer computer. The apparent "root" directory of the mapped drive will be the subfolder. | net use x : \\ computer \ sharename /user: username | Maps drive letter x : to shared folder sharename on computer computer using an alternate user's credentials. net use will prompt for a password if necessary. | net use x : \\ computer \ sharename /user: username password | As in the preceding command, but the password is specified on the command line. This is a security risk but may be your only option when mapping drives using an alternate username from a batch file. | Command | Description | net use lpt n : \\ computer \ printername | Maps a specified LPT printer port (for example, LPT2) to a network shared printer, for use by MS-DOS applications. Note: You cannot map an LPT port that corresponds to a physical printer port in your computer. | net use ... /persistent:yesno | On a net use command line, /persistent:yes stores the current and subsequent drive mapping in your profile so that the mapping is restored the next time you log on. /persistent:no makes the current and subsequent mapping appear in the current logon session only. | net use x : /d | Unmaps drive letter x :, that is, disconnects it from a shared folder. | net use lpt n : /d | Disconnects the specified LPT port from a network shared printer. | net share The net share command shares an existing folder on your computer to the network, or cancels file sharing. The most important variations are: Command | Description | net share sharename drive:path | Shares the specified folder under the specified share name | net share sharename /delete | Cancels the specified share | Additional options can specify how files are cached if a remote user requests offline access to the shared folder; type net help share for details. net start and net stop net start and net stop have nothing to do with networking. Instead, they can start and stop Windows services on your computer. The argument after start or stop is the "short" name of the service. Note Each Windows service has a "short name" (also called the "service name") and a "long name" (also called the "display name"). The Services management window lists only the display names. To see the short names of the services on your computer, type the command sc query . The sc command is a more comprehensive command-line service manager, which is also worth learning about; see "Managing Services from the Command Line" in Chapter 6 for more information. I've found this command to be most helpful on Windows Server computers, where it's easier to type net stop dns net start dns at a command prompt than to use Computer Management to restart the service when it's gone haywire. findstr findstr searches text files and folders for strings, optionally using a powerful pattern matching scheme called regular expressions . (If you're familiar with UNIX or Linux, findstr is a lot like grep .) Findstr has a slew of options. You can see the whole list by opening a command prompt window and typing help findstr , which prints the following: FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/P] [/F:file] [/C:string] [/G:file] [/D:dir list] [/A:color attributes] [/OFF[LINE]] strings [[drive:][path]filename[...]] /B Matches pattern if at the beginning of a line. /E Matches pattern if at the end of a line. /L Uses search strings literally. /R Uses search strings as regular expressions. /S Searches for matching files in the current directory and all subdirectories. /I Specifies that the search is not to be case sensitive. /X Prints lines that match exactly. /V Prints only lines that do not contain a match. /N Prints the line number before each line that matches. /M Prints only the filename if a file contains a match. /O Prints character offset before each matching line. /P Skip files with non-printable characters. /OFF[LINE] Do not skip files with offline attribute set. /A:attr Specifies color attribute with two hex digits. See "color /?" /F:file Reads file list from the specified file(/ stands for console). /C:string Uses specified string as a literal search string. /G:file Gets search strings from the specified file(/ stands for console). /D:dir Search a semicolon delimited list of directories strings Text to be searched for. [drive:][path]filename Specifies a file or files to search. Use spaces to separate multiple search strings unless the argument is prefixed with /C. For example, 'FINDSTR "hello there" x.y' searches for "hello" or "there" in file x.y. 'FINDSTR /C:"hello there" x.y' searches for "hello there" in file x.y. In the form findstr /c: "whatever string you want to find" filename... findstr scans through every file named on the command line (you can use wildcards), and prints out each line that contains the exact string. Some of the more useful options that you can put on the command line between findstr and /c are /B | Matches the string only if occurs at the beginning of the text line. findstr /b /c:"mouse" will match the line "mouse ate my cheese" but not "my house has a mouse." | /E | Like /B , but makes findstr print lines only if the search text is found at the end of the input line. | /I | Ignores case; lines with mouse, MOUSE, or Mouse will match /c:"mouse" . | /S | Searches for files in the current directory and all subdirectories. This is especially useful if you specify the filenames with wildcards, for example: findstr /s /c:"mouse" *.txt | findstr can also search for complex patterns using a pattern matching formula called a regular expression . Regular expressions are complex and beyond our scope here, but if you need to pull information out of or rearrange text files, they're an extremely powerful tool. You can find lots of information about them on the Net. The regular expressions used by findstr can use any of the following elements: Pattern item | Matches... | . | Any one character | * | Zero, one, or more occurrences of the item immediately before * | ^ | Beginning of the line; ^abc matches abc only if it occurs at the beginning of a line | $ | End of the line; abc$ matches abc only if it occurs at the end of a line | [ xyz ] | Any one character listed in the set of characters between the brackets | [^ xyz ] | Any one character not in the set of characters listed between the brackets | [ x - y ] | Any one character in the range from x to y | \< | The beginning of a word (that is, printing text preceded by whitespace or a line break) | \> | The end of a word (that is, whitespace following printing characters or a line break) | x | The character x | \ x | The character x , where x would otherwise have special meaning, that is, . * ^ $ [] or \ | For full information on findstr regular expressions refer to the online Command Reference. |