src/Get-EnvVarPathItem.ps1
#!/usr/bin/env pwsh $ErrorActionPreference = "Stop" Set-StrictMode -Version Latest <# .SYNOPSIS Gets the path entries of the specified PATH-style environment variable at the given environment variable scope. .PARAMETER Process Gets the path entries of the environment variable at the Process-level environment variable scope. .PARAMETER User Gets the path entries of the environment variable at the User-level environment variable scope. .PARAMETER Machine Gets the path entries of the environment variable at the Machine-level environment variable scope. .PARAMETER Scope The scope of the environment variable from which the path entries will be retrieved. .PARAMETER Name The exact name of the environment variable from which the path entries will be retrieved. .EXAMPLE Get-EnvVarPathItem -Process -Name "PATH" .EXAMPLE Get-EnvVarPathItem -Process -Name "PSModulePath" .COMPONENT env #> function Get-EnvVarPathItem() { [CmdletBinding(DefaultParameterSetName = "ProcessScopeForValue")] param( [Parameter(Mandatory=$true, ParameterSetName="MachineScopeForValue", Position=0)] [switch] $Machine, [Parameter(Mandatory=$true, ParameterSetName="ProcessScopeForValue", Position=0)] [switch] $Process, [Parameter(Mandatory=$true, ParameterSetName="UserScopeForValue", Position=0)] [switch] $User, [Parameter(Mandatory=$true, ParameterSetName="ScopeValueForValue", Position=0)] [System.EnvironmentVariableTarget] $Scope, [Parameter(Mandatory=$true, Position=1)] [ValidateLength(1, [int]::MaxValue)] [string] $Name ) Begin { if ($Machine -and $Machine.IsPresent) { $Scope = [System.EnvironmentVariableTarget]::Machine } elseif ($Process -and $Process.IsPresent) { $Scope = [System.EnvironmentVariableTarget]::Process } elseif ($User -and $User.IsPresent) { $Scope = [System.EnvironmentVariableTarget]::User } if (-not [System.EnvironmentVariableTarget]::IsDefined($Scope)) { throw "Unrecognized EnvironmentVariableTarget '$Scope'" } } Process { [string] $extantPath = Get-EnvVar -Scope $Scope -Name $Name -Value [string[]] $pathItems = $extantPath -split [System.IO.Path]::PathSeparator foreach ($pathItem in $pathItems) { Write-Output $pathItem } } } |