Bookmark and Share Subscribe Bookmark and Share

Categories

Advertisement



Windows 10 PATH Environment Variable Update with PowerShell

Dec
25


 « »    

Testing Environment Information:

  • Windows 10 Enterprise: Version 1909 (OS Build 18396.535) (Type winver or systeminfo at a Command Prompt)
  • PowerShell Version: 5.1.18362.145 (Type “HOST” at a PowerShell Prompt)
  • Testing Date: 12/25/2019

Background:

Unfortunately, when using the following PowerShell command, it erases all other entries in your PATH Environment Variable. Most forums I’ve seen are telling people to do this, good thing I backed up my paths before trying this method, as all my paths got erased.

[Environment]::SetEnvironmentVariable("PATH","C:\Testing","Machine")

Let’s see if we can fix this, first let us exam our current paths we have configured (it’s good to take a backup of this configuration before you make any changes, you can do that with the following PowerShell command:

$env:path | Out-File C:\EnvironmentVariableBackup.txt

Script:

Let’s create a whole script now so we can update the PATH Environment Variable and be able to use it in the future. I saved this to a file named UpdatePathEnvironmentVariable.ps1.

# Separate multiple paths with semi-colon ;
$NewPathsToAdd = "C:\Testing;C\Testing2" 

# Save this to a text file in case you need it later, for now we'll output it to the console
$CurrentPaths = $env:path 
Write-Host "`r`n `$CurrentPaths variable contains the following paths: $($currentPaths)" -ForegroundColor Cyan

# Combine the old path and new path(s) into one string, you can swap these variables around if you want the new paths at the bottom
$PathUpdate = $NewPathsToAdd + ";" + $CurrentPaths
Write-Host "`r`n `$PathUpdate variable will load the following paths: $($PathUpdate)" -ForegroundColor Cyan

# The last parameter ("Machine"),should be either User or Machine, depending on where you want the variable stored
[Environment]::SetEnvironmentVariable("PATH","$PathUpdate","Machine")




    Did I save you time and headaches? Buy me a cup of coffee.
    The more coffee I drink the more articles I can write.