PowerShell | Set Path in Windows

Intro | Add Environment Variables to Path with PowerShell

You can permanently add a folder to the PATH environment variable with PowerShell in a safe and controlled manner. PowerShell is already installed in every Windows system. Note that MS-DOS (command prompt) cannot achieve this unless you are using Windows 10.

If you want to add a folder to your path temporarily you can use the SET command. Without PowerShell you are stuck with the dangerous My Computer » Properties » Advanced » Environment Variables. Using PowerShell you can Read, Create and Remove environment variables.

How to Add to the Path in Windows 2000, Windows 7, Windows 8 and Windows 10

PowerShell will safely add to your Path environment variable so that you can type in a command from any directory. Here are the high level steps to follow

  1. Right click to open PowerShell as an Administrator
  2. Read the current path into a currentPath variable
  3. Create an updatedPath variable concatenating current and new path
  4. Finally set the PATH to be the updatedPath variable
  5. Verify by reading Windows Path and then reboot

1. How to Open Powershell as an Administrator

To open PowerShell as an administrator do the following

  1. Press Windows button and select All Program
  2. Click on Accessories and click on Windows PowerShell
  3. Right click on the option that says only Windows PowerShell
  4. And select Run as Administrator

To give yourself a warm feeling, copy and paste this command to list your Path variable.
Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH

Well done! That wasn’t too hard? Your PATHs are neatly listed. Click image for readable version. In case you need it your PowerShell executable is likely to be at this location C:\Windows\System32\WindowsPowerShell\v1.0

PowerShell command the lists the environment variables SET on the PATH

PowerShell command that lists the environment variables on the Windows machine.

2. Read Current Path into Local Variable

Due to PowerShell’s object orientation we can amend the above command by adding a call to the object’s “Path” attribute. You do it like this (–Object–).Path

PowerShell’s power comes from object orientation. You can safely ignore this fact unless you use OO with languages like Java, C# and SmallTalk. If so you can leverage your OO know-how to flatten the PowerShell learning curve.

To read the PATH environment variable you copy the below command, hover over the PowerShell window, right click, paste and [Enter].
(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).Path

Well done again! This time just the contents of your PATH environment variable is shown. This is the thing you want to add a new folder path to.

The Windows PATH environment variable is printed out to the screen.

With this PowerShell command just the content of of the Windows PATH environment variable is shown.

To copy the path contents into the local $theCurrentPath copy this command and paste it into your PowerShell script window. Press enter.

$theCurrentPath=(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).Path

You can check that the current path is indeed populated correctly with the simple Return command.

Return $theCurrentPath

3. Create the Updated Path into a Local Variable

This is where you type in the path to the program folder (or bin folder).
$theUpdatedPath=$theCurrentPath+’;C:\Program Files\...\....\bin’

Enter your path then copy this command and paste it into your PowerShell script window and press enter.

PowerShell’s concatenate folder operation must be checked to ensure that your updated path is an amalgam of the current PATH environment variable and the folder to be permanently added to the PATH. To check it run Return $theUpdatedPath

Ensure the directory to be added to the PATH environment variable is attached to the end.

4. Update PATH to be the $theUpdatedPath variable

The next command will permanently add the new directory to the PATH environment variable with a concatenate operation. Ensure you have a backup of the current path. Then confidently run the below PowerShell command.

Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH –Value $theUpdatedPath

Now your new PATH environment variable is in operation.

If access is denied it may be because you need to run PowerShell as an administrator. You do this by right clicking the executable and selecting “Run as Administrator”. More is Explained Here »

5. Verify by reading the new PATH environment variable

You may need to restart PowerShell to view the changes to the PATH environment variable. Just try rerunning the earlier command to verify our actions.
(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).Path

Now before you go to MS-DOS to type set – it pays to reboot.

I’m sure it worked – now it’s lights out and away you go.

Leave a Reply

Your email address will not be published. Required fields are marked *