Windows 10 PATH Environment Variable Update with PowerShell |
Dec
25
|
« Update Enom Domain Name via PowerShell (Dynamic DNS Update) | Install Docker Desktop for Windows on Windows 10 Enterprise » |
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.
1 | [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:
1 | $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.
1 2 3 4 5 6 7 8 9 10 11 12 13 | # 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" ) |