Managing the Command Path
Windows uses the command path to locate executables. You can view
the current command path for executables by using the PATH command. In
a command shell, type path on a line by itself, and then press Enter. In a Windows PowerShell console, type $env:path
on a line by itself, and then press Enter. In the output from either
technique, observe that Windows uses a semicolon (;) to separate
individual paths, marking where one file path ends and another begins.
The command path is set during logon by using system and user
environment variables. The path defined in the PATH system variable
sets the base path. The path defined in the PATH user variable adds to
the base path by using the following syntax:
%PATH%;AdditionalPaths
Here, %PATH% tells Windows to insert the current system paths, and AdditionalPaths designates the additional user-specific paths to use.
Caution
An improperly set path can cause severe problems. You should always
test any command path change before using it in a live environment. The
command path is set during logon. Therefore, you must log off and then
log on again to see the effects of the revised path.
Don’t forget about the search
order that Windows uses. Paths are searched in order, with the last
path in the PATH user variable being the last one searched. This can
sometimes slow the execution of your programs and scripts. To help
Windows find your programs and scripts faster, you should consider
placing a required path earlier in the search order.
Be careful when setting the command path. It is easy to overwrite
all path information accidentally. For example, if you don’t specify
%PATH% when setting the user path, you will delete all other path
information. One way to ensure that you can easily re-create the
command path is to keep a copy of the command path in a file.
-
When you are working with the command prompt, you can write the current command path to a file by entering path > orig_path.txt.
Keep in mind that if you are using a standard command prompt rather
than an administrator command prompt, you won’t be able to write to
secure system locations. In this case, you can write to a subdirectory
to which you have access or to your personal profile. To write the command path to the command-shell window, type path. -
When you are working with the PowerShell console, you can write the current command path to a file by entering $env:path > orig_path.txt.
If you are using a standard console rather than an administrator
console, you won’t be able to write to secure system locations. In this
case, you can write to a subdirectory to which you have access or to
your personal profile. To write the command path to the PowerShell
window, type $env:path.
At the command prompt or in the PowerShell window, you can modify
the command path by using the Setx.exe utility. You also can edit the
command path by completing the following steps:
-
In Control Panel, tap or click System And Security, and then tap or click System. -
In the System console, tap or click Change Settings, or tap or click Advanced System Settings in the left pane. -
On the Advanced tab in the System Properties dialog box, tap or click Environment Variables. -
Select the PATH variable in the System Variables list. Under System Variables, tap or click Edit. -
By default, the path value is selected. Without pressing any other
key, press the Right Arrow key. This should remove the selection
highlight and place the insertion point at the end of the variable
value. -
Type a semicolon, and then enter a path to insert. Repeat this step as necessary, and then tap or click OK three times.
In Group Policy, you can use a preference item to modify the command path. Follow these steps:
-
Open a Group Policy Object for editing in the Group Policy
Management Editor. To configure preferences for computers, expand
Computer Configuration\Preferences\Windows Settings, and then select
Environment. To configure preferences for users, expand User
Configuration\Preferences\Windows Settings, and then select Environment. -
Press and hold or right-click the Environment node, point to New,
and then tap or click Environment Variable. This opens the New
Environment Properties dialog box. -
In the Action list, select Update to update the path variable, or
select Replace to delete and then re-create the path variable. Next,
select User Variable to work with user variables. -
In the Name box, type Path. In the Value box, type the variable value. Typically, you’ll enter %PATH%;
followed by the paths you want to add, using a semicolon to separate
each path. If the affected computers have existing PATH user variable
definitions, you must provide the related paths to ensure that these
paths are retained. -
Use the options on the Common tab to control how the preference is
applied. In most cases, you’ll want to create the PATH variable only
once (rather than have Group Policy re-create the variable each time
policy is refreshed). If so, select Apply Once And Do Not Reapply. -
Tap or click OK. The next time policy is refreshed, the preference
item will be applied as appropriate for the Group Policy Object in
which you defined the preference item.
Caution
Incorrectly setting the path can cause serious problems. Before
deploying an updated path to multiple computers, you should test the
configuration. One way to do this is to create a Group Policy Object in
Active Directory that applies only to an isolated test computer. Next,
create a preference item for this Group Policy Object, and then wait
for a policy to refresh or apply policy using GPUpdate. If you are
logged on to the computer, you need to log off and then log back on
before you can confirm the results.
Managing File Extensions and File Associations
File extensions and file associations also are important for
determining how applications run. The types of files that Windows
considers to be executables are determined by the file extensions for
executables. File extensions allow users to execute a command
by using just the command name. File associations are what allow users
to double-tap or double-click a file and open the file automatically in
a related application. Two types of file extensions are used:
-
File extensions for executables Executable files are defined with the %PATHEXT%
environment variable and can be set using the Environment Variables
dialog box or with Group Policy preference items in much the same way
as the PATH variable. You can view the current settings by typing set pathext at the command line or by typing $env:pathext
at a PowerShell prompt. The default setting is
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC. With
this setting, the command line knows which files are executable and
which files are not, so you don’t have to specify the file extension at
the command line. -
File extensions for applications File extensions for applications are referred to as file associations.
File associations are what enable you to pass arguments to executables
and to open documents, worksheets, or other application files by
double-tapping or double-clicking their file icons. Each known
extension on a system has a file association that you can view at a
command prompt by typing assoc followed by the extension, such as assoc .doc or assoc .docx.
Each file association in turn specifies the file type for the file
extension. This can be viewed at a command prompt by typing ftype
followed by the file association, such as ftype Word.Document.8 or ftype Word.Document.12.
Note
Assoc and Ftype are internal commands for the command shell (Cmd.exe). To use the Assoc command in PowerShell, enter cmd /c assoc followed by the extension, such as cmd /c assoc .doc. To use the Ftype command in PowerShell, enter cmd /c ftype followed by the file association, such as cmd /c ftype Word.Document.8.
With executables, the order of file extensions in the %PATHEXT%
variable sets the search order used by the command line on a
per-directory basis. Thus, if a particular directory in the command
path has multiple executables that match the command name provided, a
.com file would be executed before an .exe file, and so on.
Every known file extension on a system has a corresponding file
association and file type—even extensions for executables. In some
cases, the file type is the extension text without the period followed
by the keyword file, such as cmdfile, exefile, or batfile, and the file
association specifies that the first parameter passed is the command
name and that other parameters should be passed on to the application.
For example, if you type assoc .exe to see the file associations for .exe executables, you then type ftype exefile. You’ll see the file association is set to the following:
exefile="%1" %*
Thus, when you run an .exe file, Windows knows that the first value
is the command that you want to run and anything else provided is a
parameter to pass along.
File associations and types are maintained in the Windows registry
and can be set using the Assoc and Ftype commands, respectively. To
create the file association at the command line, type assoc followed by the extension setting, such as assoc .pl=perlfile.
To create the file type at the command line, set the file-type mapping,
including how to use parameters supplied with the command name, such as
ftype perlfile=C:\Perl\Bin\Perl.exe “%1” %*.
You also can associate a file type or protocol with a specific application by completing the following steps:
-
In Control Panel, tap or click Programs. Under Default Programs, tap
or click Make A File Type Always Open In A Specific Program. -
On the Set Associations page, current file associations are listed
by file extension and the current default for that extension. To change
the file association for an extension, tap or click the file extension,
and then tap or click Change Program. -
Do one of the following:
-
In the How Do You Want To Open This Type Of File? dialog box,
programs registered in the operating system as supporting files with
the selected extension are listed automatically. Simply tap or click a
recommended program to set it as the default for the selected extension. -
To view other available programs, click More Options to view other
programs that might also support the selected extension. Tap or click a
program to set it as the default for the selected extension.
Alternatively, tap or click one of the Look For An App options to
locate another program to use as the default.
In Group Policy, you can use a preference item to create new file
types and file associations. To create a preference item for a new file
type, follow these steps:
-
Open a Group Policy Object for editing in the Group Policy
Management Editor. Expand Computer Configuration\Preferences\Control
Panel Settings, and then select Folder Options. -
Press and hold or right-click the Folder Options node, point to New, and then tap or click File Type. This opens the New File Type Properties dialog box. -
In the Action list, select Create, Update, Replace, or Delete. You would use the Delete action to create a preference that removes an existing file type preference. -
In the File Extension box, type the extension of the file type without the period, such as pl. -
In the Associated Class list, select a registered class to associate with the file type. -
Use the options on the Common tab to control how the preference is
applied. In most cases, you’ll want to create the new variable only
once. If so, select Apply Once And Do Not Reapply. -
Tap or click OK. The next time policy is refreshed, the preference
item will be applied as appropriate for the Group Policy Object in
which you defined the preference item.
To create a preference item for a new file association, follow these steps:
-
Open a Group Policy Object for editing in the Group Policy
Management Editor. Expand User Configuration\Preferences\Control Panel
Settings, and then select Folder Options. -
Press and hold or right-click the Folder Options node, point to New,
and then tap or click Open With. This opens the New Open With
Properties dialog box. -
In the Action list, select Create, Update, Replace, or Delete. -
In the File Extension box, type the extension of the file type without the period, such as pl. -
Tap or click the options button to the right of the Associated
Program box, and then use the Open dialog box to select the program to
associate with the file type. -
Optionally, select Set As Default to make the associated program the
default for files with the previously specified file extension. -
Use the options on the Common tab to control how the preference is
applied. In most cases, you’ll want to create the new variable only
once. If so, select Apply Once And Do Not Reapply. -
Tap or click OK. The next time policy is refreshed, the preference
item will be applied as appropriate for the Group Policy Object in
which you defined the preference item.
|