Private/PathHelpers.ps1
|
function Test-IsAdmin { <# .SYNOPSIS Returns $true if running as Administrator, $false otherwise. #> [CmdletBinding()] param() return ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) } function Get-PathEntries { <# .SYNOPSIS Retrieves all PATH entries for both User and Machine scopes. #> [CmdletBinding()] param( [ValidateSet('User', 'Machine', 'Both')] [string]$Target = 'Both' ) $result = @{ User = @() Machine = @() } if ($Target -eq 'User' -or $Target -eq 'Both') { $userPath = [Environment]::GetEnvironmentVariable('PATH', 'User') if ($userPath) { $result.User = $userPath -split ';' | Where-Object { $_ -ne '' } } } if ($Target -eq 'Machine' -or $Target -eq 'Both') { $machinePath = [Environment]::GetEnvironmentVariable('PATH', 'Machine') if ($machinePath) { $result.Machine = $machinePath -split ';' | Where-Object { $_ -ne '' } } } return $result } function Test-PathEntry { <# .SYNOPSIS Tests if a PATH entry exists and is valid. #> [CmdletBinding()] param( [Parameter(Mandatory)] [string]$Path ) # Expand environment variables $expandedPath = [Environment]::ExpandEnvironmentVariables($Path) $exists = $false try { $exists = Test-Path -LiteralPath $expandedPath -PathType Container -ErrorAction Stop } catch { # Access denied or other errors - treat as inaccessible $exists = $false } return @{ Original = $Path Expanded = $expandedPath Exists = $exists } } function Find-DuplicatePaths { <# .SYNOPSIS Finds duplicate PATH entries (case-insensitive). #> [CmdletBinding()] param( [Parameter(Mandatory)] [string[]]$Paths ) $seen = @{} $duplicates = @() foreach ($path in $Paths) { $normalized = $path.TrimEnd('\').ToLowerInvariant() $expanded = [Environment]::ExpandEnvironmentVariables($normalized) if ($seen.ContainsKey($expanded)) { $duplicates += @{ Path = $path DuplicateOf = $seen[$expanded] } } else { $seen[$expanded] = $path } } return $duplicates } function Get-PathCharacterCount { <# .SYNOPSIS Gets the total character count of a PATH string. #> [CmdletBinding()] param( [string[]]$Paths ) if ($null -eq $Paths -or $Paths.Count -eq 0) { return 0 } return ($Paths -join ';').Length } |