Determine installed PowerShell version

前端 未结 19 2588
半阙折子戏
半阙折子戏 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 10:09

    To determine if PowerShell is installed, you can check the registry for the existence of

    HKEY_LOCAL_MACHINE\Software\Microsoft\PowerShell\1\Install
    

    and

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\3
    

    and, if it exists, whether the value is 1 (for installed), as detailed in the blog post Check if PowerShell installed and version.

    To determine the version of PowerShell that is installed, you can check the registry keys

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine\PowerShellVersion
    

    and

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\3\PowerShellEngine\PowerShellVersion
    

    To determine the version of PowerShell that is installed from a .ps1 script, you can use the following one-liner, as detailed on PowerShell.com in Which PowerShell Version Am I Running.

    $isV2 = test-path variable:\psversiontable
    

    The same site also gives a function to return the version:

    function Get-PSVersion {
        if (test-path variable:psversiontable) {$psversiontable.psversion} else {[version]"1.0.0.0"}
    }
    

提交回复
热议问题