Public/Test-DevVmPersistentPath.ps1
|
<#
.SYNOPSIS Test if a runtime is present in the user's persistent PATH. .DESCRIPTION Test-DevVmPersistentPath checks whether the specified runtime base path exists in the user's PATH environment variable. Returns $true if the runtime is found in the user PATH, $false otherwise. .PARAMETER BasePath The base installation path for the runtime to check. .EXAMPLE Test-DevVmPersistentPath -BasePath C:\node Returns $true if any Node.js path is in the user's PATH. .EXAMPLE if (Test-DevVmPersistentPath -BasePath C:\java) { Write-Host "Java is in persistent PATH" } Check if Java is in the user's PATH and display a message. .OUTPUTS System.Boolean #> function Test-DevVmPersistentPath { [OutputType([System.Boolean])] param( [Parameter(Mandatory)] [string]$BasePath ) $userPath = [Environment]::GetEnvironmentVariable('Path', 'User') if (-not $userPath) { return $false } $baseLower = $BasePath.ToLower() $pathArray = @($userPath -split ';' | Where-Object { $_ }) $pathMatches = @($pathArray | Where-Object { $_.ToLower() -like "$baseLower*" }) return $pathMatches.Count -gt 0 } |