Determine installed PowerShell version

前端 未结 19 2702
半阙折子戏
半阙折子戏 2020-11-22 09:40

How can I determine what version of PowerShell is installed on a computer, and indeed if it is installed at all?

19条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 10:10

    This is the top search result for "Batch file get powershell version", so I'd like to provide a basic example of how to do conditional flow in a batch file depending on the powershell version

    Generic example

    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.
    )
    

    Real world example

    powershell "exit $PSVersionTable.PSVersion.Major"
    if %errorlevel% GEQ 5 (
        rem Unzip archive automatically
        powershell Expand-Archive Compressed.zip
    ) else (
        rem Make the user unzip, because lazy
        echo Please unzip Compressed.zip prior to continuing...
        pause
    )
    

提交回复
热议问题