Skip to content

Instantly share code, notes, and snippets.

@martin12333
Forked from kavaliro/SortandSetPath.ps1
Created July 20, 2021 17:36
Show Gist options
  • Save martin12333/ef509f41b0a80a9abfbe47f1b3ed2996 to your computer and use it in GitHub Desktop.
Save martin12333/ef509f41b0a80a9abfbe47f1b3ed2996 to your computer and use it in GitHub Desktop.
A script demonstrating how to sort your path entries via powershell. It's more for show. The last two lines are all that you need to accomplish it. The rest is just how one might get there.
#What's my path?
$env:Path
#How many characters long is my path?
$env:Path.Length
#I want to see each entry separately. I'll save the entries into an array...
$pathEntries = $env:Path.Split(";")
#...and output that array to the commandline.
$pathEntries
#for grins, how many entries in the array?
$pathEntries.Length
#I want to save my current path out to a file. Backup! The "|" pipes the output of one command to the input of the next. the ~ translates to "my home directory."
$env:Path.Split(";") | Out-File ~\path.txt
#Sorting the entries alphabetically
$env:Path.Split(";") | Sort-Object
#Sorting them by number of characters in the entry.
$env:Path.Split(";") | sort Length
#outputting to a file after sorting.
$env:Path.Split(";") | sort | Out-File ~\path2.txt
#What type of object is $pathEntries?
$pathEntries.GetType()
#Ok, so it's an array. What about the first object in the array?
$pathEntries[0].GetType()
#Some of the entries have whitespace. Let's get rid of that.
$pathEntries[0].Trim()
#but that would only get rid of it in the commandline output, and only for the first entry in the array.
#let's try it again. This time, we'll grab the path, split it, and pipe that into a foreach loop.
# $_ is a variable with all input to the pipe. So we go through each array entry and trim the whitespace.
#then we save that to $pathEntries.
$pathEntries = $env:Path.Split(";") | foreach{$_.Trim()}
#Now, pathEntries should be split and trimmed.
$pathEntries
#OK, so let's add a couple more details to this. I want to remove any empty lines. I want to remove any duplicates, and any empty entries.
#To remove dupes, I'll use sort's "-unique" flag. To remove empty entries, I'll use .Net's System.StringSplitOptions.RemoveEmptyEntries. The
#syntax is a little different than C#, but it's the same call.
$arry = $env:Path.Split(";",[System.StringSplitOptions]::RemoveEmptyEntries) | foreach{$_.Trim()} | sort -unique
#output the results to the commandline to make sure we have what we want.
$arry
#Now let's join it all back together. Again we will use .Net.
$proposedPath = [String]::Join(";",$arry)
#let's see the results:
$proposedPath
#This is what we want, so let's push it back out to the PATH variable.
$env:Path = $proposedPath
#In the end, all you actually need to accomplish all of this are these 2 lines:
$arry = $env:Path.Split(";",[System.StringSplitOptions]::RemoveEmptyEntries) | foreach{$_.Trim()} | sort -unique
$env:Path = [String]::Join(";",$arry)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment