Public/Get-DevVmPersistentPathEntry.ps1
|
<#
.SYNOPSIS Get the PATH entry for a runtime from the user's persistent PATH. .DESCRIPTION Get-DevVmPersistentPathEntry retrieves the first PATH entry matching the specified runtime base path from the user's PATH environment variable. Returns the matching PATH entry or $null if not found. .PARAMETER BasePath The base installation path for the runtime. .EXAMPLE Get-DevVmPersistentPathEntry -BasePath C:\node Returns the Node.js PATH entry, e.g., "C:\node\18.16.0\bin" .EXAMPLE $javaPath = Get-DevVmPersistentPathEntry -BasePath C:\java if ($javaPath) { Write-Host "Java PATH entry: $javaPath" } Get the Java PATH entry and display it if found. .OUTPUTS System.String #> function Get-DevVmPersistentPathEntry { [OutputType([System.String])] param( [Parameter(Mandatory)] [string]$BasePath ) $userPath = [Environment]::GetEnvironmentVariable('Path', 'User') if (-not $userPath) { return $null } $baseLower = $BasePath.ToLower() $pathArray = @($userPath -split ';' | Where-Object { $_ }) $match = $pathArray | Where-Object { $_.ToLower() -like "$baseLower*" } | Select-Object -First 1 return $match } |