During automation there will be a time where are specific powershell version will be required. In order to properly detect this you must use the following object: “$PSVersionTable.PSVersion” DO NOT use $Host.Version
and (Get-Host).Version
as they show the host version not the engine version. Here is a quick example of how to use this in practice.
1 2 3 4 5 6 7 8 |
$isPS3orHigher = $PSVersionTable.PSVersion.Major if ($isPS3orHigher -lt 3) { Write-Host "PowerShell 3.0 is not being used please download from: https://www.microsoft.com/en-us/download/details.aspx?id=34595" -ForegroundColor red -backgroundcolor black Write-Host "Press any key to continue ..." $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") Exit } |
PSVersion has a few properties in it, we are using Major to just return the number and then use that to compare to the lowest version that is compatible. Hope this helps!