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 a directory occurs multiple times, it is marked with an x.
If he user does not have certain privileges on the directory, it is marked with A.
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.7a
#
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 {

         try {
            $err = $null
            if (test-path -pathType container $p_ -ev err) {
                $flagExists = ' '
            }
            else {
                $flagExists = '!'
            }
         }
         catch [System.UnauthorizedAccessException] {
	 #
	 #  TODO: in Powershell 7, this does not work anymore as intended.
	 #  Rather, it pollutes the output with a message like
	 #      Access to the path 'C:\Users\xyz\AppData\Local\Microsoft\WindowsApps' is denied.
	 #
            $flagExists = 'A'
         }
         catch {
            write-host $err
         }

         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.
V0.7 Catch System.UnauthorizedAccessException
V0.7a A comment about a breaking change introduced with PowerShell 7.

See also

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

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1758200740, '216.73.216.150', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/tools/scripts/personal/paths(161): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78