functionGet-PSVersion { if (test-path variable:psversiontable) {$psversiontable.psversion} else {[version]"1.0.0.0"} }
脚本中判断PowerShell版本并执行相应代码
1 2 3 4 5 6 7 8
if ($PSVersionTable.PSVersion.Major -eq5) { #Execute code available in PowerShell 5, like Compress Write-Host"You are running PowerShell version 5" } else { #Use a different process Write-Host"This is version $PSVersionTable.PSVersion.Major" }
批处理文件中根据PowerShell版本进行条件执行
1 2 3 4 5 6
powershell "exit $PSVersionTable.PSVersion.Major" if %errorlevel% GEQ 5 ( echo Do some fancy stuff that only powershell v5 or higher supports ) else ( echo Functionality not support by current powershell version. )