Removing Duplicate Entries From %PATH%

How-to Clean Up Your %PATH%

As developers, we frequently end up with duplicate entries in our path. From the command line you can clean up your path using pathman.exe. Here’s a PowerShell Script to find the duplicates and remove them using Pathman.exe:

$extraPath=(($env:path.Split(';') | group | ?{$_.Count -gt 1}).Values | %{$_[0]} pathman.exe /ru $extrapath

Disclaimer: Works for us.

Join the Conversation

4 Comments

Your email address will not be published. Required fields are marked *

  1. Here is a “cute” way to do it in plain CMD shell script:

    I called the script “CleanupPath.cmd” and you just run it and your path duplicates will be removed without changing the order of the path.

    @setlocal EnableExtensions EnableDelayedExpansion
    @set _PATH_=
    @for %%a in (“%PATH:;=” “%”) do @if not “%%~a” == “” (
    @if “!_PATH_!” == “” @set “_PATH_=;%%~a;”
    @set “_T_=!_PATH_:;%%~a;=x!”
    @if “!_T_!” == “!_PATH_!” @set “_PATH_=!_PATH_!%%~a;”
    )
    @endlocal && @set “PATH=%_PATH_:~1,-1%

    1. !!!! Thank you! I changed the right-double-quotes and left-double-quotes to just plain double-quotes and this worked on the first try!!!