1. Understanding Variables
A variable is simply a placeholder for an actual
value. You don’t have to know the actual value as long as you know the
placeholder. For example, the location of the Windows Server 2008
operating system folder is usually c:\Windows, but it can be on another
drive and even have a different name. However, the %systemroot% variable always points to the actual location of the operating system folder.
For example, if you want to see the path to the sysvol folder, you can use this command:
Tip
Variables always start and end with a percent symbol (%). You can easily see the value of any variable by using the echo command followed by the variable.
Some of the commonly referenced variables are shown in the following table.
Variable | Description |
---|
| Shows the path to the Windows folder (typically C:\Windows). |
%programfiles% | Shows the path to the Program Files folder (typically C:\Program Files). |
%systemdrive% | Returns the drive of the root directory (typically C:\). |
%appdata% | Returns the location where applications store data by default. |
%userdomain% | Gives the name of the domain that contains the currently logged-on user’s account. |
%logonserver% | Lists the name of the domain controller that validated the current logon session (when the system is joined to a domain). |
%processor_architecture% | Returns the architecture of the processor (such as x856 for 32-bit or AMD64 for 64-bit processors). |
%userprofile% | Lists the location of the profile for the current user. |
%allusersprofile% | Lists the location of the All Users Profile. |
%cd% | Lists the current directory string. |
%date% | Returns the current date. |
%time% | Returns the current time. |
%errorlevel% | Gives the error number of the last executed command.
Anything other than 0 indicates an error occurred. |
Tip
You can use variables in commands at the command
prompt. For example, if you want to open the Windows update log
(windowsupdate.log), you can use the following command:
notepad %systemroot%\windowsupdate.log
Tip
You can view a listing of all variables with the set command.
It’s also possible to create your own variables. This can sometimes be useful in scripts.
Variable | Description |
---|
set variable-name = value
C:\>set myvariable = test
| Creates the variable and assigns a value. |
C:\>echo %myvariable%
Test
| Shows the value of the variable. |
2. Understanding Switches
You can modify most commands by using one or more
switches. A switch is preceded by a space and a forward slash (/) or a
space and a dash (-). For example, if you want to flush the DNS cache,
you use the ipconfig command and modify it with the /flushdns switch like this:
Some commands can use either a forward slash or a dash, whereas others work only with one or the other.
Command | Description |
---|
C:\>ipconfig -flushdns
C:\>ipconfig /flushdns
| Both of these commands work the same. |
| This works. Even though the space is omitted before the slash, the ipconfig command recognizes it. |
ipconfig-flushdns
is not recognized as an internal or external command, operable program, or batch file. | This fails. The ipconfig command doesn’t recognize the dash (-) as a switch without a space. |
Tip
It’s best to always use a space before the
switch because this consistently works. Some commands do not work if you
use the forward slash (/) or dash (-) without a space preceding it.