Created
April 19, 2020 18:00
-
-
Save paulevans/bda23a12f3602122f4d090abbd94797f to your computer and use it in GitHub Desktop.
Tidy up Windows PATH environment variable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# For demostration purposes only. Use at your own risk. | |
$OriginalMachinePath = [Environment]::GetEnvironmentVariable('Path','Machine') | |
$UniqueMachinePaths = ($OriginalMachinePath -split ';') | ForEach-Object { | |
$_.Trim([Char]'\',[Char]'/') | |
} | Sort-Object -Unique | |
$UniqueMachinePath = [string]::Join(";",$UniqueMachinePaths).Trim([Char]';') | |
$OriginalMachinePath > ".\original_machine_path.txt" | |
$UniqueMachinePath > ".\unique_machine_path.txt" | |
$OriginalUserPath = [Environment]::GetEnvironmentVariable('Path','User') | |
$UniqueUserPaths = ($OriginalUserPath -split ';') | ForEach-Object { | |
$_.Trim([Char]'\',[Char]'/') | |
} | Sort-Object -Unique | |
$UniqueUserPath = [string]::Join(";",$UniqueMachinePaths).Trim([Char]';') | |
$FilteredUserPaths = $UniqueUserPaths.Where({-Not ($UniqueMachinePaths.contains($_))}) | |
$FilteredUserPath = [string]::Join(";",$FilteredUserPaths).Trim([Char]';') | |
$OriginalUserPath > ".\original_user_path.txt" | |
$UniqueUserPath > ".\unique_user_path.txt" | |
$FilteredUserPath > ".\filtered_user_path.txt" | |
# Use caution and at your own risk. | |
Write-Output $FilteredUserPath # It's not empty... right? | |
[Environment]::SetEnvironmentVariable('Path', $FilteredUserPath,'User') | |
# You need to be running an administrator to clean up the machine path. | |
# Also double check anything, don't trust anything you didn't write, etc, etc o_o | |
Write-Output $UniqueMachinePath # It's not empty... right? | |
[Environment]::SetEnvironmentVariable('Path', $UniqueMachinePath,'Machine') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment