Search notes:

paths.ps1

paths.ps1 prints the individual components (directories, hopefully) of the PATH or the PSModulePath environment variable on separate lines.
If a directory does not exist, it is prepended by an exclamation mark.
If the same directory occurs multiple times, it is marked with an x.
In order to print the components of PSModulePath, the -PSModulePath switch must be used.
#
# Show individual path-components of the PATH or PSModulePath environment variable, each on its own line:
#
# V0.6
#
param (
   [switch] $psModulePath
)

set-strictMode -version 3

$pathsSeen = @{}

function showPaths {
   param (
      [System.EnvironmentVariableTarget] $tgt,
      [string]                           $var
   )

   write-host $tgt

   foreach ($p in [System.Environment]::GetEnvironmentVariable($var, $tgt) -split ';' ) {

    #
    # Replace environment variables that are enclosed in %...% with their actual value.
    #
      $p_ = [regex]::Replace($p, '%([^%]+)%', {
        param($match)
        invoke-expression "`$env:$($match.Groups[1].Value)"
      })

      if ($tgt -eq 'process' -and $pathsSeen[$p_]) {
        #
        # Special case for paths in process environment because
        # this environment also contains the variables of machine and
        # user.
        # These paths need only be shown if they weren't already
        # shown.
        # Unfortunately, this solution also prevents a path
        # that occurs multiple times in the process environment
        # only from being reported multiple times.
        # 
          continue
      }

      if ($p -eq '') {
         write-host "   ! <empty>"
      }
      else {

         if (test-path -pathType container $p_) {
             $flagExists = ' '
         }
         else {
             $flagExists = '!'
         }

         if ($pathsSeen[$p_]) {
             $flagSeen = 'x'
         }
         else {
             $flagSeen = ' '
         }
          write-host "  $flagSeen$flagExists $p"
      }

      $pathsSeen[$p_] = $true
   }
}

$envVar = 'PATH'
if ($psModulePath) {
   $envVar = 'PSModulePath'
}

showPaths machine $envVar
showPaths user    $envVar
showPaths process $envVar
Github repository scripts-and-utilities, path: /paths.ps1

History

V0.2 Show paths for machine and user separately.
V0.3 Add -PSModulePath option.
V0.4 Replace environment variables that are enclosed in %…% with their actual value.
V0.5 Mark duplicate directories with an x.
V0.6 Special treatment for paths in process environment: do not show them if they were already shown when iterating over machine or user environment.

See also

paths (ordinary shell), paths.pl (Perl)
Other scripts

Index