Skip to content

Instantly share code, notes, and snippets.

@asheroto
Last active November 8, 2025 12:09
Show Gist options
  • Select an option

  • Save asheroto/fa216475272e58837b06c4be61088530 to your computer and use it in GitHub Desktop.

Select an option

Save asheroto/fa216475272e58837b06c4be61088530 to your computer and use it in GitHub Desktop.

Revisions

  1. asheroto renamed this gist Feb 9, 2022. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. asheroto revised this gist Jan 17, 2022. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -6,4 +6,14 @@ The script is **NOT** lazy - it *actually* checks each environmental variable **

    Enjoy! 😊️️❤

    ## Install Note

    You can also install this script by typing...

    ```
    Install-Script -Name Remove-DuplicatesFromPathVariables
    ```

    As available on [PowerShell Gallery](https://www.powershellgallery.com/packages/Remove-DuplicatesFromPathVariables/0.0.1).

    ![example](https://i.imgur.com/QPR2wHg.png)
  3. asheroto revised this gist Jan 17, 2022. No changes.
  4. asheroto renamed this gist Jan 17, 2022. 1 changed file with 1 addition and 1 deletion.
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@ $TempMachinePath
    # User PATH
    Write-Output ""
    Write-Output ("-" * 50)
    Write-Output "🧑 Checking System PATH 🧑"
    Write-Output "🧑 Checking User PATH 🧑"
    $TempUserPath = [System.Environment]::GetEnvironmentVariable("Path", "User");
    $Dupes = ($TempUserPath.Split(";") | Group-Object | Where-Object { $_.Count -gt 1 }).Values;
    if ($Dupes.Count -gt 0) { $Dupes | ForEach-Object { $TempUserPath = $TempUserPath.Replace("$_;", ""); $TempUserPath += "$_;" }; [System.Environment]::SetEnvironmentVariable("Path", $TempUserPath, "User") }
  5. asheroto created this gist Jan 17, 2022.
    9 changes: 9 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    # Remove Duplicate Entries from PATH Environmental Variables

    This PowerShell script will check the system PATH variable and the user PATH variable for duplicates and then remove any duplicates found.

    The script is **NOT** lazy - it *actually* checks each environmental variable **separately**, NOT simply `$ENV:PATH`.

    Enjoy! 😊️️❤

    ![example](https://i.imgur.com/QPR2wHg.png)
    45 changes: 45 additions & 0 deletions Remove-Duplicate-Entries-From-Path-Variables.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    Clear-Host;

    # Begin
    Write-Output "⏲️⏲️⏲️ Starting ⏲️⏲️⏲️"
    Write-Output ("-" * 50)
    Write-Output ""

    # System PATH
    Write-Output "🖥️ Checking System PATH 🖥️"
    $TempMachinePath = [System.Environment]::GetEnvironmentVariable("Path", "Machine");
    $Dupes = ($TempMachinePath.Split(";") | Group-Object | Where-Object { $_.Count -gt 1 }).Values;
    if ($Dupes.Count -gt 0) { $Dupes | ForEach-Object { $TempMachinePath = $TempMachinePath.Replace("$_;", ""); $TempMachinePath += "$_;" }; [System.Environment]::SetEnvironmentVariable("Path", $TempMachinePath, "Machine") }
    Write-Output @"
    $("-" * 50)
    Found duplicates
    $("-" * 50)
    $Dupes
    $("-" * 50)
    Removing duplicates, result...
    $("-" * 50)
    $TempMachinePath
    "@;

    # User PATH
    Write-Output ""
    Write-Output ("-" * 50)
    Write-Output "🧑 Checking System PATH 🧑"
    $TempUserPath = [System.Environment]::GetEnvironmentVariable("Path", "User");
    $Dupes = ($TempUserPath.Split(";") | Group-Object | Where-Object { $_.Count -gt 1 }).Values;
    if ($Dupes.Count -gt 0) { $Dupes | ForEach-Object { $TempUserPath = $TempUserPath.Replace("$_;", ""); $TempUserPath += "$_;" }; [System.Environment]::SetEnvironmentVariable("Path", $TempUserPath, "User") }
    Write-Output @"
    $("-" * 50)
    Found duplicates
    $("-" * 50)
    $Dupes
    $("-" * 50)
    Removing duplicates, result...
    $("-" * 50)
    $TempUserPath
    "@;

    # Complete
    Write-Output ""
    Write-Output ("-" * 50)
    Write-Output "✔️✔️✔️ Complete ✔️✔️✔️"