Environment Variables – PATH Shorthands
Purpose:
Create and use Environment Variables (PATH shorthands) to make entering long PATH addresses easier and more accurate.
Overview:
Environment variables are code words used to represent the PATH address of a specific folder (or file). Key folders and files can have different PATH addresses on each PC Windows is installed on, so Windows sets the actual PATH address to each Environment variable upon install. This allows the same batch or script to run on different PCs without customizing the script for each PC. Regular users can benefit from using these “PATH address shorthand” Environment variables. PATH addresses can get very long and complicated, making entering the PATH address tedious, error prone, and time consuming. Environment Variables “shorthand” codes for specific PATH addresses are easier to enter, less error prone, and easy to remember. The following instructions explain how to use the Environment variables that come with Windows, how to create your own personal Environment variables, and examples of the benefits of their use. If you find that you enter the same PATH address frequently or drill down thru File Explorer to the same folder often, then this tip will save you a lot of time and frustration.
Special Note:
This was written based upon Windows 10, but applies to Windows versions since Windows 7. Location of settings page may differ.
Quick Take:
<u>Using Environment Variables</u>
- Environment variables can be entered in any place a PATH address can be entered in Windows.
- Environment variables begin and end with the percent sign (%). Note: In PowerShell Environment variables are entered with $env: prior to variable name.
- Method 1 – Enter Environment variable into the Search bar by the Start menu.
- Method 2 – Enter Environment variable into a Run box.
- Method 3 – Enter Environment variable into a File Explorer address bar.
- Method 4 – Use an Environment variable in any command in Command Prompt, Powershell, .bat file, script, or any other command environment.
- Environment variables can stand alone as the PATH address or can be part of a PATH address.
<u>Create an Environment Variable</u>
- Open a Run box and enter: ms-settings:about
- Select Advanced system settings
- On Advanced tab select Environment Variables…
- In the User variables section select New…
- Enter a Name for the variable and the PATH address the variable is shorthand for.
- OK each window to close and save the variable.
- Reboot for changes to take effect.
Detailed Instructions:
<u>Using Environment Variables</u>
- An Environment variable is a short word(s) that is “shorthand” for a long PATH address. Example: Appdata is shorthand for C:\Users\YOU\AppData\Roaming ( YOU is your logon name).
- When Windows was installed and you set up your account, Windows created default User and System Environment variables based upon the install. See below for how to create a list of Environment variables on your PC.
- Environment variables can be used anywhere in Windows where you enter a PATH address. Several example methods explained below. Windows recognizes an Environment variable where a PATH address is entered by enclosing the Environment variable in percent signs (%). Example: %Appdata%. <u>This means anywhere you enter an Environment variable it must start with the % sign and end with the % sign for Windows to recognize it is an Environment variable, except in PowerShell.</u>
- Environment variables can be used as part of the PATH address. Example: %Appdata%\Microsoft\Templates which is way easier to enter than C:\Users\YOU\AppData\Roaming\Microsoft\Templates (opens where Office templates are stored by default)
- Method 1 Use – Click the Search bar next to the Start Menu and enter your Environment variable and press the Enter key. File Explorer will open to that PATH address for that Environment variable. Try it, type %Appdata% and press the Enter FYI: You can enter any PATH address into the Search bar and File Explorer will open to that folder or open the file if the address ends in a file.
- Method 2 Use – Open a Run box (WinKey +R), enter the Environment variable and press the Enter File Explorer will open to that PATH address for that Environment variable. Try it in the Run box, type %Appdata% and press the Enter key.
- Method 3 Use – In File Explorer, click the dropdown arrow on the right side of the address bar, which will highlight the current PATH address. Enter the Environment variable and press the Enter key. File Explorer will move to that PATH address. This also works for any Open or Save Explorer window for any application.
- Method 4 Use – Commands – Environment variables can be used in any command line application such as Command Prompt, PowerShell, .bat files, and scripts as part of the PATH address in the command. Since entry of a PATH address (including long complicated addresses) occurs most often in commands, using Environment variables in these situations demonstrates the maximum benefits of using Environment variables.
<u>Create an Environment Variable</u>
- A user can create and edit Environment variables, but should not edit the default Environment variables. Many of the default Environment variables are tiered variables where editing one can damage other variables. Edit only the variables you create.
- Open a Run box (WinKey + R) and type in ms-settings:about into the box and press the Enter This will open Windows Settings to the About page.
- Scroll down the About settings page and select Advanced system settings which will open the System Properties window with the Advanced tab already selected.
- At the bottom of the Advanced page, click the Environment variables… button to open the Environment Variables
- Click the New button under the User variables window to open the New User Variable
- In the New User Variable box enter a name in the Variable name: <u>The name you choose should be short and easy for you to remember</u>. Do not include any % signs as part of the name, so enter the name without % signs. Example: An Environment variable which will connect to the data drive on a networked PC, I use the PC’s name – MSI (for my MSI laptop). When I use it I have to enter only %MSI% in the Search bar and that networked PC’s data drive opens. In the Variable value: box enter the full PATH address to the folder or file. You can use the Browse Directory.. button to use File Explorer to go to the folder or the Browse File.. button for a file and File Explorer enter the correct PATH address for you.
- Click the OK button to <u>each</u> window to save the new Environment variable and close the About settings page.
- You must reboot the PC for any new or edited Environment variable to take effect.
Comments:
Why create and use Environment variables?
- Shortcuts don’t work in commands such as batch files. After having batch files fail to run the task because you entered the PATH address wrong by one character, you tend to spend more time making sure the PATH address is correct. Since most of the PATH addresses that I enter differ in only the final folder of the address, I created an Environment variable to the folder just before the final folder. This turned a long PATH address to an Environment variable and the final folder such as %Output%\Jan2021, where Output is to a nested folder on a networked drive.
- Modifying the Normal template of Word or other default templates for Office applications requires you open the template from within the application. This involves an Open Explorer window and drilling down to where the templates are stored, which is thru a lot of folders if you remember where they are kept. I created an Environment variable called Template for the PATH address %Appdata%\Microsoft\Templates. In Word in the Open window I click the down arrow in the address bar and type in %Template% and press the Enter key; then select the template I want to modify…don’t have to remember where or drill down.
- You can create shortcuts to any drive/folder/file, but to use those shortcuts from your Desktop they have to be on your desktop. I don’t want a bunch of shortcuts on my desktop, so I use custom Environment variables instead. I created Environment variables to folder locations I visit often. %Photos% opens my drive where I store my photos, videos, graphics, etc. %How% opens my archives of computer tips, info, articles, etc. (aka How To). I have more and there is no limit (within reasonable use – there are always limits) to how many you can make. Entering these into the Search bar is just as quick to me as finding and clicking a shortcut and way faster than going thru File Explorer.
- Custom Environment variable can make editing batch files unnecessary. I have batch files that run automatically. When I have to change the folder/files the function will work on (input) or change the location for files the function will output, I have to edit the PATH address in each command in each batch file. I created Environment variables %Input% and %Output% with the current PATH addresses I wanted and used these variables in the commands in the batch files. When I need to change either location, I edit the Environment variable and leave the batch files alone…I enter the new PATH address only once.
- If you do a fair amount of entering PATH addresses, then environment variables will make your life easier. If you rarely enter PATH addresses, then environment variables will be of little use to you.
Create a List of Environment Variables For Your PC
- Open a command prompt (enter cmd in Search bar)
- At the prompt (>) enter the following command replacing the underscore (_) with a space and a PATH address for SaveLocation where you want the report saved to such as %USERPROFILE%\Documents to place in your Documents folder. Press Enter Key.
>set_>_SaveLocation\EnVar.txt
- If entered correctly the prompt will return and the report will be where you saved it.
- As you create new Environment variables they will show in a new set report.
HTH, Dana:>>
HTH, Dana:))