private/Tests/Test-InstallPwsh.ps1
|
function Test-InstallPwsh { <# .SYNOPSIS Returns $true when PowerShell 7 is installed on this machine. .DESCRIPTION Checks the well-known install path first ($env:ProgramFiles\PowerShell\7\pwsh.exe), then falls back to scanning the Uninstall registry hives for a 'PowerShell 7*' display name. .NOTES Author: David Segura Company: Recast Software Dependencies: None #> [OutputType([bool])] param () # Fast path — default install location if (Test-Path -Path (Join-Path $env:ProgramFiles 'PowerShell\7\pwsh.exe')) { return $true } # Registry fallback $regPaths = @( 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*', 'HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*' ) foreach ($regPath in $regPaths) { $entry = Get-ItemProperty -Path $regPath -ErrorAction SilentlyContinue | Where-Object { $_.PSObject.Properties['DisplayName'] -and $_.DisplayName -like 'PowerShell 7*' } | Select-Object -First 1 if ($entry) { return $true } } return $false } |