Determine installed PowerShell version

前端 未结 19 2400
半阙折子戏
半阙折子戏 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:07

    I needed to check the version of PowerShell and then run the appropriate code. Some of our servers run v5, and others v4. This means that some functions, like compress, may or may not be available.

    This is my solution:

    if ($PSVersionTable.PSVersion.Major -eq 5) {
        #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"
    }
    
    0 讨论(0)
  • 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"}
    }
    
    0 讨论(0)
  • 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
    )
    
    0 讨论(0)
  • 2020-11-22 10:12

    Use $PSVersionTable.PSVersion to determine the engine version. If the variable does not exist, it is safe to assume the engine is version 1.0.

    Note that $Host.Version and (Get-Host).Version are not reliable - they reflect the version of the host only, not the engine. PowerGUI, PowerShellPLUS, etc. are all hosting applications, and they will set the host's version to reflect their product version — which is entirely correct, but not what you're looking for.

    PS C:\> $PSVersionTable.PSVersion
    
    Major  Minor  Build  Revision
    -----  -----  -----  --------
    4      0      -1     -1
    
    0 讨论(0)
  • 2020-11-22 10:16

    Microsoft's recommended forward compatible method for checking if PowerShell is installed and determining the installed version is to look at two specific registry keys. I've reproduced the details here in case the link breaks.

    According to the linked page:

    Depending on any other registry key(s), or version of PowerShell.exe or the location of PowerShell.exe is not guaranteed to work in the long term.

    To check if any version of PowerShell is installed, check for the following value in the registry:

    • Key Location: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1
    • Value Name: Install
    • Value Type: REG_DWORD
    • Value Data: 0x00000001 (1

    To check whether version 1.0 or 2.0 of PowerShell is installed, check for the following value in the registry:

    • Key Location: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine
    • Value Name: PowerShellVersion
    • Value Type: REG_SZ
    • Value Data: <1.0 | 2.0>
    0 讨论(0)
  • 2020-11-22 10:16

    $host.version is just plain wrong/unreliable. This gives you the version of the hosting executable (powershell.exe, powergui.exe, powershell_ise.exe, powershellplus.exe etc) and not the version of the engine itself.

    The engine version is contained in $psversiontable.psversion. For PowerShell 1.0, this variable does not exist, so obviously if this variable is not available it is entirely safe to assume the engine is 1.0, obviously.

    0 讨论(0)
提交回复
热议问题