5.7. Managing Environment Variables with the Set Command
The term environment variable
is foreign to many users because environment variables work in the
background to make life easier for both GUI users and those who work at
the command line. Admittedly, someone working at the command line is
more likely to see the effects of environment variables directly. For
example, without a proper path environment variable, you can't execute
many applications. The path points to the locations where the command
processor should look for applications.
Windows supports
two forms of environment variables: permanent and session. The session
environment variables only affect the current command line session. As
soon as you close the command window, these environment variables are
gone. You create session environment variables using the Set
command. Permanent environment variables exist for all Windows GUI
applications as well as the command prompt. Unless you change them,
permanent environment variables exist between reboots of your system as
well. You set permanent environment variables using the WMIC Environment
alias . For example, if you want to set the permanent path variable to include the C:\Temp directory, you would type WMIC Environment Where Name="Path" SET VariableValue="C:\Temp;%PATH%" and press Enter. Notice that you include the %PATH% expansion variable to preserve the existing path information.
NOTE
Many administrators
know that they can set environmental variables using the Environment
Variables dialog box. To access this dialog box in a GUI version of
Windows, right-click My Computer and choose Properties from the context
menu. Select the Advanced tab of the System Properties dialog box. Click
Environment Variables to display the Environment Variables dialog box.
Unfortunately, Server Core lacks the interface elements required to use
this option.
When you work at the command line, you often need environment variables in addition to those Windows uses. For example, the COPYCMD
environment variable affects the Copy, Move, and XCopy utilities. You
can set the command line switches you want to use with these utilities
by setting the COPYCMD environment
variable. Of course, you can always override your selections by
specifying a different set of command line switches at the command line.
For example, if you set the /Y command line switch in the COPYCMD environment variable, you can override it by specifying the /-Y command line switch.
Many environment variables only affect one application. For example, you can set the MORE
environment variable to set the command line switches for the More
utility. Every time you use the More utility, it searches for this
environment variable and sets itself up accordingly.
Command line
environment variables need not impose on those used in Windows. Besides
setting environment variables in the Environment Variables dialog box
and at the command line using the Set command, you can add environment variables to the AutoExec.NT file. Windows executes the commands within AutoExec.NT
every time it opens a command prompt for you. These environment
variables appear every time you open a command prompt, but won't appear
within Windows.
For all of the tasks that it performs, the Set command is relatively simple. This command uses the following syntax:
SET [variable[=[string]]]
SET /A expression
SET /P variable=[promptString]
The following list describes each of the command line arguments.
variable
Specifies
the environment variable name. The name can't contain spaces or most
special characters. You can split words in a variable name using the
underscore (_) or dash (-).
string
Defines the value of the environment variable. If you type the environment variable, followed by an equals sign (=), but without a value, the Set command deletes the environment variable. Type the Set
command followed by the environment variable without an equals sign to
determine the current environment variable value. In fact, you can type
as little as a single letter to see a list of environment variables and
their associated values that begin with that letter.
/A
expressionCreates an
environment variable based on an expression, such as a math equation or
the concatenation of multiple environment variables, instead of a
standard string.
/P
variable=[promptString]Prompts the user
to assign a value to an environment variable, rather than assigning the
value directly. You must supply a variable name. The optional prompt
string lets you provide a specific prompt to the user. Otherwise, the
display doesn't show any prompt at all.
Server Core comes with a
number of standard environment variables. These hard-coded environment
variables perform essential tasks, such as displaying the current
directory for you. You can see a list of these environment variables on
the Microsoft Web site at http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds_shelloverview.mspx. Typing Set by itself and pressing Enter displays the list of environment variables set for your machine as shown in Figure 2. This list doesn't include the hard-coded environment variables such as CD.
You can use the /A
command line switch to combine existing environmental variables or even
perform math with them. For example, you could use the following Set command to create a new environment variable based on the existing %NUMBER_OF_PROCESSORS% environment variable.
SET /A TwiceTheProcessor=%NUMBER_OF_PROCESSORS% * 2
You can use any of the operators shown in Table 4 when creating your expression. The operators appear in order of precedence.
Table 4. Set Expression Operators
Operator | Description |
---|
() | Group expression elements |
! ~ - | Not, negate, and negative unary operators |
* / % | Multiply, divide, and modulus arithmetic operators |
+ - | Add and subtract arithmetic operators |
<< >> | Right and left logical shift |
& | Bitwise AND |
^ | Bitwise exclusive OR |
| | Bitwise OR |
= *= /= %= += -= &= ^= |= <<= >>= | Assignment operators |
, | Expression separator |
Any expressions you
create can also contain octal, decimal, or hexadecimal numbers. All
octal values begin with a 0 (zero) and hexadecimal values begin with a
0x.
5.8. Managing Environment Variables with the SetX Utility
The SetX utility performs essentially the same tasks as the Set command . However, it offers considerably more
flexibility because you can use SetX over a network to interact with
variables on other machines. In addition, it offers considerable
flexibility in setting variables, such as letting you choose between the
current user and the system as a whole. Rather than repeat all of the
common information for using SetX, please refer to the Set command as a starting point. This command uses the following syntax:
SETX [/S system [/U [domain\]user [/P [password]]]] var value [/M]
SETX [/S system [/U [domain\]user [/P [password]]]] var /K regpath [/M]
SETX [/S system [/U [domain\]user [/P [password]]]] /F file {var
{/A x,y | /R x,y string}[/M] | /X} [/D delimiters]
The following list describes each of the command line arguments.
/S
systemSpecifies the remote system that you want to check. In most cases, you'll also need to supply the /U and the /P command line switches when using this switch.
/U
[domain\]userSpecifies the
username on the remote system. This name may not match the username on
the local system. You'll need to supply a domain name when working with a
domain controller.
/P
[password]Specifies the
password for the given user. You can provide the command line switch
without specifying the password on the command line in cleartext. The
system prompts you for the password. Using this feature can help you
maintain the security of passwords used on your system.
var
Contains the name of the variable you wish to change.
value
Sets the specified
variable to the supplied value. You don't have to provide the equals
sign (=) as you do when working with the Set command. Place values with spaces within double quotes.
/M
Sets the value as a system-wide (machine-level) variable that appears in HKEY_LOCAL_MACHINE. The default setting places the variable within the current user's environment.
/K
regpathSets the
specified variable using the content of a registry key. You must provide
the fully qualified registry value (including all of the keys), such as
KEY_LOCAL_MACHINE\System\CurrentControlSet\Control\TimeZoneInformation\StandardName.
/F
fileSpecifies the name
of a text file to use for settings. You must also provide the position
of the setting within the file as an absolute or relative coordinate
(line number and character position). In addition, if the setting is
delimited, you must provide the delimiter used for it.
/A
x,yProvides the
absolute position of the setting within the file. You must provide the
position as a line number and character number.
/R
x,y stringProvides a
relative position of the setting within the file. SetX first searches
for the string you specify within the file. It then moves relative to
that string the number of lines and characters within the line that you
specify.
/X
Displays the
file contents using x and y coordinates. This argument doesn't actually
set any variables. What it does is display the position of each
potential setting within the file so that you can use the /A and /R arguments with greater ease.
/D
delimitersDefines the
delimiters used to begin and end settings entries within the file. The
default settings include space, tab, carriage return, and linefeed. You
can use any ASCII character as a delimiter. The maximum number of
delimiters, including the default delimiters, is 15.
5.9. Executing Applications Using the Start Command
A common way to handle this problem, when the application supports
it, is to ask it to return immediately and continue working in the
background. The only problem with this approach is that you aren't sure
that the command or utility completed successfully. The Start
command helps you overcome this problem by creating a new window for
the command or window to run in. The command or utility still controls
the command prompt until it completes, but since it runs in another
window, the user can continue working.
Another use for this
utility is to start the command or utility in a window with a specific
title. You can apply settings to a window that rely on the window title . Whenever you open a window with that title, it also has
the special formatting that you specified. In short, you can create
custom environments in which to run a utility. This utility uses the
following syntax:
START ["title"] [/Dpath] [/I] [/MIN] [/MAX] [/SEPARATE | /SHARED] [/LOW
| /NORMAL | /HIGH | /REALTIME | /ABOVENORMAL | /BELOWNORMAL] [/WAIT]
[/B] [{command | program}] [parameters]
The following list describes each of the command line arguments.
"title"
Defines the title of the command window title bar.
/D
PathSpecifies the
starting directory for the command window. This value doesn't have to
be the same as the path for the command that you want to execute. For
example, it might be the path to the data directory for the command.
/B
Starts the
application without creating a new window. The application executes as a
background task and doesn't display a user interface.
/I
Passes the
standard command environment to the new window, rather than using any
changes made to the command environment by the current command window.
Using this command line switch ensures that the application won't run in
a contaminated environment where an environmental setting could
adversely affect the way the application runs.
/MIN
Starts the application with the command window minimized.
/MAX
Starts the application with the command window maximized.
/SEPARATE
Starts 16-bit
Windows applications in a separate memory space. Normally, Windows uses a
separate memory space for all 32-bit applications, but a single memory
space for all 16-bit applications. Older applications don't always use
memory correctly, resulting in memory corruption that can cause other
applications to fail. Using this command line switch eliminates shared
memory corruption problems. However, this feature comes at the cost of
performance and overall system resource usage. Unlike 32-bit memory
spaces, 16-bit memory spaces remain fixed in memory, which uses the
memory inefficiently and causes memory fragmentation. Memory
fragmentation can cause Windows to perform inefficiently, leading to
performance problems. In short, use this command line switch only when
you actually experience problems.
/SHARED
Starts the
16-bit Windows application in a shared memory space. This is the default
setting and you should normally use it unless you experience memory
corruption problems while running the application.
/LOW
Starts the
application in the IDLE priority class. Use this priority setting for
applications that only run when other applications don't require the
processor. This command line switch helps the system function more
efficiently when you want to run the application as a background task
and don't care when it completes its work.
/BELOWNORMAL
Starts the
application in the BELOWNORMAL priority class. Use this priority setting
for background applications that you want to complete in a timely
manner, but not at the expense of foreground applications.
/NORMAL
Starts the
application in the NORMAL priority class. This is the default setting.
Foreground applications normally start at this priority. You should only
use this setting for applications that you want to complete at a normal
pace. This setting does affect all foreground application
responsiveness.
/ABOVENORMAL
Starts the
application in the ABOVENORMAL priority class. This setting places the
application at a slightly higher priority than standard applications.
The other foreground tasks continue to run, but at a noticeably slower
pace. Use this setting for priority applications that must complete
tasks quickly.
/HIGH
Starts the
application in the HIGH priority class. You won't normally have a good
reason to use this priority for any application. Using this priority
level can affect system functionality and definitely slows other
foreground applications to a crawl. Always use this setting with extreme
caution.
/REALTIME
Starts the
application in the REALTIME priority class. Using this priority level
stops execution of other foreground tasks and some system tasks as well.
In addition, system functionality degrades noticeably. In some
circumstances, the system could actually freeze and require a reboot.
Generally, you don't want to use this priority for any reason.
/WAIT
Starts the application and waits for it to terminate. This setting ensures that you know when an application completes its task.
{command | program}
Specifies the
internal command/batch file or the external utility/ application to run.
The command window remains visible after you run an internal command or
batch file because the system executes the command processor (CMD.EXE) with the /K switch. The system runs external utilities and
applications in a window or in a full-screen console. Unless you change
the default behavior using a command line switch, the window or
full-screen console closes when the application completes execution.
parameters
Specifies the parameters (arguments) passed to the command or program.
5.10. Determining the Operating System Version with the Ver Command
The Ver command
is one of the easier commands to use. It doesn't require any arguments
and provides only one type of output. Whenever you use the Ver
command, you receive the operating system version information. The
version information contains the operating system name in human-readable
form, the major version number, the minor version number, and the build
number. For example, the current Windows XP version at the time of this
writing is Microsoft Windows XP [Version 5.1.2600]. Server Core
displays something like Microsoft Windows [Version 6.0.6001].
5.11. Getting Volume Information with the Vol Command
The Vol
command displays the volume information for the current or selected
drive. The information includes the drive letter, the drive volume name,
and the serial number. This command uses the following syntax:
VOL [drive:]
The following describes the command line argument.
drive Specifies the letter of the drive for which you want to obtain volume information. The default is the current drive.
6. Using Debugging Utilities
The command line is an
admittedly simple place to work from a programming perspective. In fact,
using batch files is more along the lines of working with a very simple
kind of script, rather than a complex application. Even so, you'll
eventually need to perform some kind of debugging. The debugging may
involve checking inside a file to ensure you aren't going to execute a
virus. You may also have to perform some low-level tasks with very old
applications to get them to run in the Server Core environment. The
following sections describe the debugging utilities that Server Core
makes available for your use.
6.1. Examining, Modifying, and Debugging Files with the Debug Utility
Microsoft
originally intended the Debug utility to help developers debug assembly
language applications. You can still use the utility for that purpose,
but the number of assembly language programs still in use is very small.
In fact, it's likely that you'll never see an application originally
written in assembler.
Fortunately, the Debug
utility does have other purposes. For example, you can use it to view
hidden information in executable files. Application developers leave
many notes inside applications and it's often helpful to view these
messages when deciding on the alternative uses of a utility. You can
also locate copyright information in files when you want to know more
about the file. Sometimes, you can also use the Debug utility to locate
information in data files or make quick fixes to damaged data files.
Most people refer to this other use for Debug as hex (short for
hexadecimal) editing. However, the Debug utility interface is archaic
and many people find it hard to use. If you need low-level access to
files regularly, you might want to invest in a third-party utility for
the task such as XVI32 . This utility uses the following syntax:
DEBUG [[drive:][path]filename [testfile-parameters]]
The following list describes each of the command line arguments.
[drive:][path]filename
Specifies the name of the file that you want to view, edit, or debug.
testfile-parameters
Specifies
the command line arguments required by the application you want to test.
You never use this second argument to simply view or edit a file.
6.2. Converting Executables with the Exe2Bin Utility
The Exe2Bin
utility converts an application from the Portable Executable (PE) format
to the binary format. The main difference is in how the executable
loads. At some time in the past, you needed to use this utility quite
often to convert executable files because the linkers of the time didn't
provide the required functionality, but now it's a relic that you might
never use. The resulting binary file is smaller than the executable and
may execute slightly faster. At a time when developers counted
individual bits, this utility served an important purpose. This utility
uses the following syntax:
EXE2BIN [drive1:][path1]input-file [[drive2:][path2]output-file]
The following list describes each of the command line arguments.
[drive:][path1]input-file
Specifies the
location and name of the EXE file you want to convert. If you don't
provide drive and path information, the utility assumes that you want to
use the current drive and path.
[drive1:][path1]output-file
Specifies the
location and name of the binary file (usually with a COM extension) that
you want to output. If you don't provide drive and path information,
the utility assumes that you want to use the current drive and path.