7. A Sampling of Commands
The following sections
provide an introduction to some of the things you can do at the command
prompt. They are by no means all the commands that you'll come across.
As you read through these, I strongly encourage you to try out each of
the commands at the command prompt.
7.1. Dir
The DIR command
provides a list of contents from the current directory. This is an easy
way to view a list of files in this directory, and it also includes a
list of directories in the current directory. Some commonly used
switches are shown in Table 2.
Table 2. DIR switches
Switch | Comments and Examples |
---|
/A:x | Lists files with specific attributes. Use D for directories, H for hidden files, S for system files, R for read-only files, A for files ready for archiving, I for files not indexed, and L
for reparse points. (A reparse point is an NTFS extended object, such
as a mount point, which looks like a folder but points to another
drive.)
DIR /A:H shows hidden files. |
/S | Displays files in subdirectories within the current directory.
DIR *.exe /S lists all the executables in the current directory or any subdirectories. |
/W | Displays the contents in a wide format that is sometimes easier to read.
DIR /W |
/P | Pauses the output if it scrolls for more than a page. |
7.2. Copy
The Copy command can be used to copy files from the command prompt. When the Copy command is used, the original file is not modified and a new file is created. It has the following format:
Copy sourceFile destinationFile
Both the sourceFile and the destinationFile
can include a path statement. If the path is omitted, the current path
is used. If the file is being copied to a different path, the destinationFile name can be omitted and the name will be the same.
As an example, you can copy a file named test.txt to a file named test2.txt in the current directory with this command:
Copy test.txt test2.txt
If the test.txt file exists in a folder named Docs on the C: drive and you want to copy it to the C:\Data folder keeping the original name, use this command:
Copy C:\docs\test.txt c:\data
You can also use wildcards with Copy commands. For example, if you wanted to copy all of the files from the C:\Data folder to the C:\Archive folder, you could use this command:
Copy C:\data\*.* c:\archive\
As explained earlier, the asterisk (*) wildcard can be used in place of zero or more characters. When you use *.*, it will include all files in the folder.
Similarly, if you were already in the C:\Archive
folder, you could use the following command, omitting the destination.
All of the files would be copied from the source folder and copied into
the current folder.
Copy C:\data\*.*
7.3. XCopy
One of the problems with the Copy command is that it can't copy subdirectories. XCopy can copy subdirectories, and that's one of the primary reasons why it's used.
The format of the XCopy command is similar to that of the Copy command:
xCopy sourceFile destinationFile
Some of the common switches used with XCopy are listed in Table 3.
Table 3. XCopy switches
Switch | Comments |
---|
/S | Copies
both directories and subdirectories except empty ones. The following
command copies all of the files and subdirectories from the current
folder to the C:\Archive folder.
XCopy *.* c:\Archive\ /S |
/E | Copies
both directories and subdirectories, including empty ones. The
following command copies all of the files and subdirectories from the
current folder to the C:\Archive folder.
XCopy *.* c:\Archive\ /E |
/Y /-Y | /Y suppresses prompting to confirm you want to overwrite files. This can be useful in scripts.
XCopy *.* c:\Archive\ /S /Y /-Y turns prompting back on. |
/C | Continues copying even if errors occur.
XCopy *.* c:\Archive\ /S /C |
/Q | Quiet mode. Does not display files while copying.
XCopy *.* c:\Archive\ /S /Q |
7.4. SET
The SET command was
used earlier to show and modify the path, but it can be used to view
and/or modify many of the environment variables. Entering SET with no parameters will show many of the system variables and their current value:
SET
Listing 3 shows the output of the Set command on my computer.
Example 3. Output of the Set command
ALLUSERSPROFILE=C:\ProgramData APPDATA=C:\Users\Darril\AppData\Roaming CommonProgramFiles=C:\Program Files\Common Files CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files CommonProgramW6432=C:\Program Files\Common Files COMPUTERNAME=DRG ComSpec=C:\Windows\system32\cmd.exe FP_NO_HOST_CHECK=NO HOMEDRIVE=C: HOMEPATH=\Users\Darril LOCALAPPDATA=C:\Users\Darril\AppData\Local LOGONSERVER=\\DRG NUMBER_OF_PROCESSORS=2 OS=Windows_NT Path=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem; c:\Program Files (x86)\Microsoft SQL Server\90\Tools\binn\; C:\Windows\System32 \WindowsPowerShell\v1.0\; C:\Windows\System32\Windows System Resource Manager\bin; C:\Windows\idmu\common PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
PROCESSOR_ARCHITECTURE=AMD64 PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 23 Stepping 6, GenuineIntel PROCESSOR_LEVEL=6 PROCESSOR_REVISION=1706 ProgramData=C:\ProgramData ProgramFiles=C:\Program Files ProgramFiles(x86)=C:\Program Files (x86) ProgramW6432=C:\Program Files PROMPT=$P$G PSModulePath=C:\Windows\system32\WindowsPowerShell\v1.0\Modules\
PUBLIC=C:\Users\Public SESSIONNAME=Console SystemDrive=C: SystemRoot=C:\Windows TEMP=C:\Users\Darril\AppData\Local\Temp TMP=C:\Users\Darril\AppData\Local\Temp USERDOMAIN=DRG USERNAME=Darril USERPROFILE=C:\Users\Darril VS80COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio 8\Common7\Tools\ windir=C:\Windows
|
You can narrow the search by
using the first letter of the variable in which you're interested. For
example, to see all the variables that start with the letter C, you can
use the following command:
SET C
7.5. NET USE
The NET command includes several subcommands, but one of the most basic and frequently used NET commands is NET USE. The NET USE
command can be used to connect to shares on remote computers. It does
this by mapping an unused drive letter (such as Z:) to a Universal
Naming Convention (UNC) path of \\ServerName\ShareName.
In other words, if you have a server named Srv1 that has a share named Data, you can use the UNC path of \\Srv1\Data. Use the following command to map this UNC path to the Z: drive:
Net Use Z: \\Srv1\Data
Now you can change the drive name to Z: by simply typing Z:
and pressing Enter, and you'll have access to the UNC path from the
command prompt. You can copy files to or from the Z: drive (as long as
you have permission).
When you've finished, you should return the environment to normal and delete the mapping. The following command will do so:
Net Use Z: /delete
7.6. SystemInfo
The SystemInfo
command will output a significant amount of information on the system.
This can be very valuable when troubleshooting a system because it gives
you a lot of information at a glance. It can quickly tell you things
like these and more:
Hostname
Operating system name, edition, and version
When it was installed and when it was last booted
Details on the hardware such as BIOS version, number, and type of processors and how much RAM is installed
Names of hotfixes that have been installed
Listing 4 shows the output of this command on my system.
Example 4. Output of the SystemInfo command
Host Name: DRG OS Name: Microsoft Windows 7 Ultimate OS Version: 6.1.7600 N/A Build 7600 OS Manufacturer: Microsoft Corporation OS Configuration: Standalone Workstation OS Build Type: Multiprocessor Free Registered Owner: Darril Gibson Registered Organization: SY0-201.com Product ID: 00426-065-0543977-86656 Original Install Date: 8/9/2009, 4:42:31 AM System Boot Time: 9/21/2009, 12:46:35 PM System Manufacturer: Hewlett-Packard System Model: HP Pavilion dv7 Notebook PC System Type: x64-based PC Processor(s): 1 Processor(s) Installed. [01]: Intel64 Family 6 Model 23 Stepping 6 GenuineIntel ~2401 Mhz BIOS Version: Hewlett-Packard F.26, 2/6/2009 Windows Directory: C:\Windows System Directory: C:\Windows\system32 Boot Device: \Device\HarddiskVolume1 System Locale: en-us;English (United States) Input Locale: en-us;English (United States) Time Zone: (UTC-05:00) Eastern Time (US & Canada) Total Physical Memory: 6,111 MB Available Physical Memory: 969 MB Virtual Memory: Max Size: 13,799 MB Virtual Memory: Available: 8,529 MB Virtual Memory: In Use: 5,270 MB Page File Location(s): C:\pagefile.sys Domain: WORKGROUP Logon Server: \\DRG Hotfix(s): 3 Hotfix(s) Installed.
[01]: KB958559 [02]: KB972636 [03]: KB973874 Network Card(s): 1 NIC(s) Installed. [01]: Intel(R) PRO/100 VE Network Connection Connection Name: Local Area Connection DHCP Enabled: Yes DHCP Server: 192.168.1.1 IP address(es) [01]: 192.168.1.103 [02]: fe80::bd37:44ef:a2c5:47d2
|
As with any command, you can send the output of this command to a text file using the redirection symbol, like this:
Systeminfo > SystemInfo.txt
7.7. DriverQuery
The DriverQuery command
can be used to list all of the installed device drivers. The drivers
are listed with their name, the type of driver, and when they were
installed. The basic syntax is
DriveryQuery
That seems simple enough, but if
you do it, you'll notice that there are so many drivers listed that if
fills the screen buffer, and you can't even scroll back to where the DriverQuery command was entered. However, you can get around this in a couple of ways.
Use the | More command. The symbol before the word More
is called pipe, so it's read as "pipe more" or sometimes referred to as
pipelining. Pipelining allows you to combine two commands and have the
output of one as the input to another in this case. The symbol is
usually located above the Enter key and is typed by using
Shift+backslash (\). Enter it like this:
DriverQuery | More
The More
command will display a screen at a time. You can press the spacebar to
get another screen or press the Enter key to get the next line.
You can modify the screen
buffer size as a second method to ensure you can view all of the data
provided by the command. Right-click the title bar of the command prompt
and select Properties. Select the Layout tab. Change the Screen Buffer
Size Height value to 9999 and click OK.
Also, just as you saw with the SystemInfo command, you can redirect the output to a text file.
7.8. Echo
Echo can be used to
display text on the screen. You can use this to provide some type of
information to the user from a batch file. The basic syntax is
Echo Your Message
When included in a batch
file, this command will display in entirety and then display the
message, all of which can be a little distracting. As an example, if a
batch file has the Echo Hello line, it will output the following text:
Echo Hello
Hello
You can easily turn off the first line (Echo Hello) with the Echo off command. Even the Echo Off command can be suppressed by adding the at symbol, like this:
@Echo off
If you want to display a blank line, add a period after Echo without any spaces, like this:
Echo.
To put all of this together,
you can use the following code in a batch file. The first line
suppresses the echo (including the command that suppresses echo), and
the second line outputs the message "Hello." The third line creates a
blank line, and the last line outputs another message.
@Echo of
Echo Hello
Echo.
Echo Let's get started
When this batch file is executed, it will output the following three lines (with the middle line blank).
Hello
Let's get started