if ($PSVersionTable.PSVersion.Major -eq 5) {#Execute code available in PowerShell 5, like CompressWrite-Host "You are running PowerShell version 5"}else {#Use a different processWrite-Host "This is version $PSVersionTable.PSVersion.Major"}
这是“Batch file get PowerShell version”的热门搜索结果,因此我想提供一个基本示例,说明如何根据PowerShell版本在批处理文件中执行条件流
通用示例
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.)
现实世界的例子
powershell "exit $PSVersionTable.PSVersion.Major"if %errorlevel% GEQ 5 (rem Unzip archive automaticallypowershell Expand-Archive Compressed.zip) else (rem Make the user unzip, because lazyecho Please unzip Compressed.zip prior to continuing...pause)
Path Name Type Data---- ---- ---- ----HKLM:\SOFTWARE\Microsoft\PowerShellCore\InstalledVersions\31ab5147-9a97-4452-8443-d9709f0516e1 SemanticVersion String 7.1.3