Networking Environment variables
The command shell makes several environment variables available to commands.
Environment variables all begin and end with percent signs. You can use an
environment variable anywhere in a command. For example,
C:>echo %OS% running on a %PROCESSOR_IDENTIFIER%
displays a line such as this:
Windows_NT running on an x86 Family 15 Model 2 Stepping 8, GenuineIntel
Interestingly, Windows NT, Windows 2000 Server, Windows Server 2003, and
Windows Server 2008 all display "Windows_NT" for the operating system name.
If the environment variable represents a path, you may need to enclose it in
quotation marks, like this:
C:>dir "%HOMEPATH%"
This command displays the contents of the user's home directory. The quotation
marks are required here because the environme>nt variable expands to a pathname that may include spaces, and the command shell requires that long filenames that include spaces must be enclosed in quotation marks.
Table-1 lists the environment variables that are available to you and your
commands.
Variable | Description |
%ALLUSERSPROFILE% |
The location of the All Users
profile
|
%APPDATA% |
The path where applications
store data by default
|
%CD% |
The path to the current
directory
|
%CMDCMDLINE% |
The command line that was
used to start the command
shell
|
%CMDEXTVERSION% |
The version number of the
command shell
|
%COMPUTERNAME% | The computer's name |
%COMSPEC% |
The path to the command
shell executable (cmd.exe)
|
%DATE% |
The current date in the
format generated by the date
/t command
|
%ERRORLEVEL% |
The error returned by the
most recent command
|
%HOMEDRIVE% |
The drive letter of the user's
home directory
|
%HOMEPATH% |
The path to the user's home
directory
|
%HOMESHARE% |
The network path to the
user's shared home directory
|
%LOGONSERVER% |
The name of the domain
controller the user logged on
to
|
%NUMBER_OF_PROCESSORS% |
The number of processors on
the computer
|
%OS% |
The name of the operating
system
|
%PATH% | The current search path |
"%PATHEXT%" |
A list of the extensions the
operating system treats as
executable files
|
%PROCESSOR_ARCHITECTURE% |
The chip architecture of the
processor
|
%PROCESSOR_IDENTIFIER% |
A description of the
processor
|
%PROCESSOR_REVISION% |
The revision level of the
processor
|
%PROMPT% | The current prompt string |
%RANDOM% |
A random number between 1
and 32,767
|
%SYSTEMDRIVE% |
The drive containing the
operating system
|
%SYSTEMROOT% |
The path to the operating
system
|
%TEMP% |
The path to a temporary
folder for temporary files
|
%TMP% | Same as %TEMP% |
%TIME% |
The time in the format
produced by the time /t
command
|
%USERDOMAIN% |
The name of the user's
domain
|
%USERNAME% | The user's account name |
%USERPROFILE% | The path to the user's profile |
%WINDIR% |
The path to the operating
system directory
|
Batch files
A batch file is simply a text file that contains one or more commands. Batch files
are given the extension .bat and can be run from a command prompt as if they
were commands or programs. You can also run a batch file from the Start menu
by choosing Start → Run, typing the name of the batch file, and clicking OK.
As a network administrator, you'll find plenty of uses for batch files. Most of them
won't be very complicated. For example, here are some examples of very simple
batch files:
- Used a one-line file to copy the entire contents of an important shared network drive to a user's computer every night at 10 p.m. The user had just purchased a new Dell computer with a 100GB drive, and the server had only a 20GB drive. The user wanted a quick-and-dirty backup solution that would complement the regular tape backups that ran every night.
- Used a pair of short batch files to stop and then restart Exchange server before and after nightly backups for backup software that didn't have an Exchange plug-in that could back up the mail store while it was open.
- If frequently need to work with several related folders at once, create a short batch file that opens Explorer windows for each of the folders. (You can open an Explorer window from a batch file simply by typing the path to the folder that you want to open as a command.) Then, place the batch file on my desktop so I can get to it quickly.
You can also use batch files to create logon scripts that are executed whenever a
user logs on. Microsoft keeps trying to get users to use profiles instead of logon
scripts, but many networks still use logon scripts.