How to Use Environment Variables in Windows XPEnvironment variables have long been used in computer operating systems. Unix - based systems as well as in Windows. Environment variables are very convenient in scripts where certain standard. They include parameters such as the location of Windows and the. Windows Vista User Environment Variables In Linux
A table of some of the more useful. I have omitted some of the more technical ones. Although. the variables are shown in upper case, they are not case- sensitive. More. details are given about some of them in subsequent sections. Some more common predefined environment variables. Variable. Typical value (May vary) %ALLUSERSPROFILE%C: \Documents and Settings\All Users%APPDATA%C: \Documents and Settings\. Code is usually $P$G%SYSTEMDRIVE%The drive containing the Windows XP root directory, usually C: %SYSTEMROOT%The Windows XP root directory, usually C: \Windows%TEMP% and %TMP%C: \DOCUME~1\. Some. of these are listed in the next table. Apache Tomcat Windows Quick Start Guide Using Apache Tomcat in a Windows environment might seem simple enough, given that all Windows distributions include an. The second problem was however that neither of these had any effect on the user profile location. A bit of investigation revealed that: net user SomeUser /profilepath. Listing of Microsoft Windows Vista questions and answers. Setting Environment Variables. Using environment variables is one of the ways that Windows communicates information such as the location of system files and folders. Some dynamic environment variables. Variable. Value%DATE%Current date in the format determined by the Date command%TIME%Current time in the format determined by the Time command%CD%Current directory with its full path %ERRORLEVEL%Number defining exit status of a previous command or program%RANDOM%Random number between 0 and 3. Questions about. In the future it would. Environmental variables are used by the operating system to save settings (default values, locations of resources) to be used by Windows or by processes launched by. The PATH Environment Variable. The path to a file is basically its address on the computer. It. tells programs how to find a file. It is the drive plus any directories. The %PATH% environment variable. Typically, this is a group of directories. Examples of. the default values are listed in the first table above; in this case, they. Windows directory and two of its important system sub- directories. The PC user can also modify the contents. Adding directories to PATH can be very. For. example, it can be convenient to put the directory where you keep backups into. PATH variable. It can also be useful to modify PATH if you put programs. In the command line and in batch. PATH can be modified by the command path = dir. This command will create a PATH environment variable consisting of the three. Note that the directory names are separated. To add a directory . For permanent changes, use. It is also possible to use the command- line executable setx. Any file that is in a directory listed in the PATH variable can be found or opened. This is very handy in scripting and in. For instance, the name of an executable file that is in a. PATH variable can be entered into Start- Run to. The. file is sol. C: \Windows\System. Normally. this entire string would have to be used in order to reference the Solitaire. Because its directory is listed in the PATH variable, however, it is. In. fact, because of another environment variable PATHEXT discussed next. Start- Run is. sufficient to open the solitaire game. The PATHEXT Variable. As is explained on other pages, file extensions. Windows what it is supposed to do with a file. Certain file extensions. The %PATHEXT% environment variable contains. PATH% variable. The extension . EXE is the most. common. Thus the filename sol. If files have the same name but different extensions. EXE, . COM, . BAT, and . CMD. Adding or Editing Environment Variables. Existing variables can be edited or deleted and new ones can be added in. For temporary changes, the command . An example is: set temp=C: \temp. Here, the %TEMP% directory has been changed from the default value. Note that the there must be no spaces on either side of the . Changes made with. A more useful application. Open a command window and. A more permanent way to manage environment variables is provided in the. System Properties dialog box. Open Control Panel- Performance. Maintenance- System (or right- click on My Computer and. In the box that opens, click the . Next, click the button . The figure below shows the . It lists two kinds of variable- those that apply only. You can simply. scroll down the lists to see what is on your system or you can edit the lists. That makes it easier for me to write a backup. I have to do is enter %BACKUP% whenever I want to refer. To create a new variable, use the . The box for. adding a new user variable is shown below. Generally, this is likely to be. The maximum total size for all environment variables, including. Be sure to remember to separate directory names with a semicolon. If. you use programs in a particular directory a great deal, you may wish to. The figure shows that the directory . Using the add- on tool Setx. It is not part of every Windows XP setup but a command- line tool called setx. Windows XP Service Pack 2 Support Tools. This tool extends the set command so that permanent changes in the environment variables can be made. For example, to add a folder C: \New Folder to the path, the command would be: setx path . The command might be. C: \env. Microsoft also has a VBScript that lists environment variables on this page. Registry Keys for Environment Variables. For those who are experienced with editing the Registry, there is another. User environment variables. Registry in the key: HKEY. Editing the Registry is primarily for. PC user. Setting Environment Variables in Autoexec. The file autoexec. DOS and older versions of Windows. For the most part, Windows XP will. Although environment variables can be set in this way, there. Environment variables in Windows Vista, Windows 7, and Windows 8. The same general considerations hold but there are differences in the details of environment variables for Windows Vista, 7 and 8. These are discussed on another page. Environment Variables in Power. Shell . For example, the System. Root environment variable tells you the Windows OS installation path (e. C: \Windows) and the Path environment variable lists the directories that Windows will search for executable programs and DLL files. Unlike Windows Power. Shell, the Cmd. exe command shell doesn't have its own variables—the only variables available are environment variables. In Cmd. exe, you access environment variables by using the %variablename% syntax. For example, Cmd. Path% with the content of the Path environment variable. Power. Shell also provides access to environment variables through the Env: drive. For example, the command. Get- Child. Item Env: returns all environment variables (i. Env: drive). You can also use the Get- Item cmdlet to retrieve a particular environment variable. For example, the command. Get- Item Env: Pathoutputs the name of the environment variable (e. Path) and its value. Like Cmd. exe, Power. Shell supports string replacement of an environment variable name with its value, but Power. Shell doesn't use the %variablename% syntax. Power. Shell uses the syntax $Env: variablename instead (e. Env: Path). Environment variables are separate from Power. Shell variables. Power. Shell variables are subject to Power. Shell scopes, but environment variables aren't. Scope refers to where a variable is visible in Power. Shell. Figure 1 shows an example. The $my. Name variable doesn't exist outside of the script. The Test. 1. ps. 1 script defines the variable and uses it, but the variable disappears, or . Another way to say this is that environment variables have a global scope. The environment variable TEST doesn't exist, gets set inside a script, and is still available after the script completes. Environment variables in a Cmd. This means that all changes a shell script makes to environment variables, no matter whether it adds a new environment variable or changes an existing one, persist after the script completes. This behavior is by design, because many shell scripts rely on it. For example, Microsoft Visual Studio 2. Cmd. exe shell script named vcvarsall. C/C++ compiler. Problem 1. Power. Shell can't run a Cmd. Power. Shell can run a Cmd. This happens because environment variable changes don't propagate from the child process (i. Cmd. exe instance that runs the shell script) to the parent process (Power. Shell). Cmd. exe behavior 2. You can create a scope for environment variables in a Cmd. Setlocal command. This means that when a shell script uses the Setlocal command, any changes it makes to environment variables are reversed when the script completes. For example, variables added by the script are removed and variables changed by the script are reverted to their previous values. Problem 2. Power. Shell doesn't have a Setlocal command equivalent for creating a scope for environment variables. All environment variables have a global scope in Power. Shell. The Invoke- Cmd. Script function overcomes problem 1, and the Get- Environment and Set- Environment functions overcome problem 2. All three functions are defined in the Environment. Listing 1. The Invoke- Cmd. Script function runs a Cmd. It does this by running Cmd. Set command, which outputs a list of all environment variables and values. The function parses the shell script's output using a regular expression and uses the Set- Item cmdlet to set each environment variable in Power. Shell. Table 1 describes the patterns in the regular expression used by the function to parse the shell script's output. Environment variable names can contain any character except the equals sign. The parentheses group the environment variable's name. The environment variable's name and value are always separated by the = character. The parentheses group the environment variable's value. I use .* rather than . The Get- Environment function outputs the current environment as a collection of Dictionary. Entry objects. You could replace this function with the single command it runs (Get- Child. Item Env: ), but I wrote it as a function to make its intent clear and to provide an inverse operation to the Restore- Environment function. The Restore- Environment function requires a parameter that contains a collection of Dictionary. Entry objects (i. Get- Environment function) to compare with the current list of environment variables. The function uses the Compare- Object cmdlet twice: Once to determine if any environment variables have been added (so it can use the Remove- Item cmdlet to remove them) and once to determine if any environment variables have been changed (so it can restore the previous values). Adding a dot at the beginning of a command, like in the first command in Figure 3, is called dot- sourcing the command. Dot- sourcing runs a script in the current scope instead of creating a new scope. Next, the session sets the ENV1 environment variable to contain the string Original value. After this, it saves the current environment using the Get- Environment function. Then, it updates the ENV1 variable to the string Modified value. At this point, the session uses the Get- Content cmdlet to show the content of a Cmd. ENV2 environment variable. It then uses the Invoke- Cmd. Script function to run that script. Finally, the session uses the Restore- Environment function to restore the environment to its previous state. The last two commands in the session show that the Restore- Environment removed the ENV2 environment variable and restored the ENV1 environment variable to its previous value. Figure 4 shows another Power. Shell session that uses the functions to save, set, and restore the environment variables set by the vcvarsall. Visual Studio 2. 01. This example shows how the Get- Environment and Save- Environment functions provide scope for environment variables and how the Invoke- Cmd. Script function can run a Cmd. In addition, there's no Setlocal command equivalent in Power. Shell to revert environment variable changes. The Invoke- Cmd. Script, Get- Environment, and Save- Environment functions remove these limitations and give Power. Shell more power over environment variables.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
August 2017
Categories |