Quantcast
Channel: GARYTOWN ConfigMgr Blog
Viewing all articles
Browse latest Browse all 206

Edit (Add / Remove) System Path Variable via PowerShell

$
0
0

Ok, so I found a ton of blogs / posts out there about changing the System Path Variable in Windows, but not a way I'd want to do it, so I came up with my own script for adding / removing items.

What it does.

  • Grabs Current System Environment Path Variable and Places into a PowerShell Variable "Environment"
  • Checks for "rules" of items you want to delete, then deletes them from the PowerShell Variable
  • Adds any "Additional Items" you specify into the PowerShell Variable
  • Commits the PowerShell Variable "Environment" as the Updated System Path Variable.

In Action: (Please Note, the added and removed items are ONLY for example, I'm NOT suggesting you do this)

Code:

 

#GARYTOWN.COM @gwblok
#Script to Add and/or remove items from the System Path
 

#Get Current Path
$Environment = [System.Environment]::GetEnvironmentVariable("Path", "Machine")

#Remove "Junk" from Path
foreach ($path in ($Environment).Split(";"))
    {
    if ($path -like "*SysWOW64\WIndowsPowerShell\v1.0*")
        {
        $Environment = $Environment.Replace($Path ,"")
        }
    if ($path -like "c:\temp*")
        {
        $Environment = $Environment.Replace($Path ,"")
        }
    }

#Add Items to Environment
$AddPathItems = "C:\Windows\CCM\;C:\ProgramData\WaaS\;"
$Environment = $Environment.Insert($Environment.Length,$AddPathItems)

#Set Updated Path
[System.Environment]::SetEnvironmentVariable("Path", $Environment, "Machine")

Posted Originally on GARYTOWN.COM


Viewing all articles
Browse latest Browse all 206

Trending Articles