• Clean your Path!

    Home » Forums » AskWoody support » Windows » Windows 10 » Windows 10-other » Clean your Path!

    Author
    Topic
    #2443639

    Hey Y’all,

    I found this PowerShell script as an answer to a question on StackOverflow and thought it would be useful to our readers.

    The Path statement gets polluted over time by directories that no longer exist and duplicates because some install program just added directories that it needed w/o checking to see if they were already there.

    This little script will output a color coded list of all the Directories in the path environment variable with the active ones in green and the dead ones in red. Don’t forget to check the green ones for duplicates especially those that start with C:\Windows and %systemroot% as system root will evaluate to C:\Windows!

    Author: Panomosh @ StackOverFlow.com

    Clear-Host
    foreach($path in ((get-childitem -path env:\path | 
      Select -exp Value).split(";"))){ 
        if($Path -ne "") {
          If (Test-Path $path){
                Write-host "True  - $path" -ForegroundColor Green } 
          else {Write-host "False - $path" -ForegroundColor Red   }
      } #End Select
    }   #End For-Each

    To edit your path use:

    1. Control Panel
    2. System
    3. Advanced system settings
    4. Environment Variables…
    5. System variables (bottom Window) then select Path and Edit.

    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!
    Computer Specs

    • This topic was modified 1 year, 4 months ago by PKCano.
    • This topic was modified 1 year, 4 months ago by RetiredGeek.
    • This topic was modified 1 year, 4 months ago by RetiredGeek. Reason: Edited code to allow for BLANK paths
    2 users thanked author for this post.
    Viewing 7 reply threads
    Author
    Replies
    • #2443646

      Please could you explain how the script differentiates between a so-called “active” directory and a so-called “dead” one.

      Is it simply that a “dead” one no longer exists?
      If that’s the case, just how much extra processing time is required at fetch time to find that a directory in the path no longer exists?

      BATcher

      Plethora means a lot to me.

    • #2443884

      To prevent errors from empty path values – semicolon at the end of the path.
      Add this before the select statement and a closing brace (}) at the end of the whole command.

      if($path){

      Here’s a screenshot (I can’t post the content) and a text file containing the commands.

      Capture

      cheers, Paul

      1 user thanked author for this post.
    • #2443940

      Paul T

      I get the following error when I tried you suggestion.

      Missing closing '}' in statement block or type definition at line:6 char:2
      Unexpected token '}' in expression or statement.

      Can you please post a working example?

    • #2443986

      Please be careful to include the final “}”  at the end on a line by itself.

    • #2443975

      Can someone please post a fully-working PS script that doesn’t result in:

      Test-Path : Cannot bind argument to parameter 'Path' because it is an empty string.
      At line:3 char:21
      +        if(Test-Path $path){
      +                     ~~~~~
          + CategoryInfo          : InvalidData: (:) [Test-Path], ParameterBindingValidationException
          + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.TestPathCommand

      Many thanks.

      • #2444007

        Can someone please post a fully-working PS script that doesn’t result in:

        Test-Path : Cannot bind argument to parameter 'Path' because it is an empty string.
        At line:3 char:21
        +        if(Test-Path $path){
        +                     ~~~~~
            + CategoryInfo          : InvalidData: (:) [Test-Path], ParameterBindingValidationException
            + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.TestPathCommand

        Many thanks.

        It doesn’t seem to like when the PATH variable ends with a semicolon.

    • #2444012

      Hey Y’all,

      I updated the original script to allow for blank paths. I also added some comments to clarify the end of blocks.

      HTH

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

      1 user thanked author for this post.
    • #2444065

      BTW, anyone else getting the following error when running the script…

        False – C:\Windows\sysnative

      Even thought that’s been a legitimate path variable since Windows Vista and Server 2008

      • #2444154

        Alejr,

        Don’t know about your machine but I checked all 3 of my W10 machines and that directory does not exist. Have you checked yours?

        May the Forces of good computing be with you!

        RG

        PowerShell & VBA Rule!
        Computer Specs

        • #2444332

          C:\Windows\sysnative is a special “virtual folder” that allows 32-bit apps to access the 64 bit programs located in the C:\Windows\System32 folder instead of being automatically redirected to the C:\Windows\SysWOW64 folder.

            The Sysnative folder in 64-bit Windows explained

          As explained in the above article, it’s not visible using Windows Explorer but has been a part of every Windows 64 bit operating system since Vista/Server 2008 and is a part of my Path variable.

          I was just wondering if others are getting the same error (i.e. it’s also part of their Path) or if I can remove it from my Path without causing problems for any of the older 32 bit programs I still use?

          • #2444371

            Alejr,

            Interesting! However, I tried running the C:\Windows\Sysnative\nbtstat.exe from the article in both PowerShell as Admin and Command Prompt as Admin and neither could find the file.

            The Test-Path powershell cmdlet used to test the directories in the Path environment variable does not see this virtual folder either as shown by your testing.

            The only advice I can give you on this is to test by removing  C:\Window\Sysnative from your Path and see if your programs still run correctly. If not put it back.

            May the Forces of good computing be with you!

            RG

            PowerShell & VBA Rule!
            Computer Specs

            • #2444385

              RetiredGeek

              You’re right, if you run that command using the “native64 bit versions of cmd or powershell they’ll return “The system cannot find the path specified.

              However, as the article pointed out, sysnative is intended to allow 32 bit programs to access the 64 bit files located in the System32 folder.

              So, if you use the 32 bit versions of cmd or powershell

              C:\Windows\SysWOW64\cmd.exe
              
              C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe

              it works just fine.

              NBTSTAT

              Anyway, before I remove it from my path, I’d like to know if anyone else who’s run your script also displays a false result for sysnative.

              BTW, great script!

              I’m also a Retired Geek (22 yrs USAF Telcomm Maintenance and 16 yrs IT contractor) and my first PC was a Heathkit H8 I put together back in ’77

            • #2444562

              I don’t have that folder in the path in either cmd shell.

              cheers, Paul

        • #2444594

          Ok, things get even stranger.

          If I run your Test-Path script from the native 64 bit powershell, it flags C:\Windows\sysnative as an invalid path and passes everything else.

          I run it from the 32 bit powershell, if flags C:\Windows\System32\OpenSSH as an invalid path and passes everything else (including C:\Windows\sysnative).

          HUH

          1 user thanked author for this post.
          • #2444694

            Alejr,

            As they used to say on Laugh In “Very Interesting!”

            This may have something to do with C:\Windows\SysNative being a “Virtual” folder as described in one of the links you supplied. Perhaps the OpenSSH is also a “Virtual” folder?

            As with anything to do with computers YMMV!

            Cheers

            May the Forces of good computing be with you!

            RG

            PowerShell & VBA Rule!
            Computer Specs

    • #2444074

      Many thanks, @RetiredGeek. That fixed it.

    Viewing 7 reply threads
    Reply To: Clean your Path!

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: