Determine installed PowerShell version

前端 未结 19 2376
半阙折子戏
半阙折子戏 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 09:57

    Use:

    $psVersion = $PSVersionTable.PSVersion
    If ($psVersion)
    {
        #PowerShell Version Mapping
        $psVersionMappings = @()
        $psVersionMappings += New-Object PSObject -Property @{Name='5.1.14393.0';FriendlyName='Windows PowerShell 5.1 Preview';ApplicableOS='Windows 10 Anniversary Update'}
        $psVersionMappings += New-Object PSObject -Property @{Name='5.1.14300.1000';FriendlyName='Windows PowerShell 5.1 Preview';ApplicableOS='Windows Server 2016 Technical Preview 5'}
        $psVersionMappings += New-Object PSObject -Property @{Name='5.0.10586.494';FriendlyName='Windows PowerShell 5 RTM';ApplicableOS='Windows 10 1511 + KB3172985 1607'}
        $psVersionMappings += New-Object PSObject -Property @{Name='5.0.10586.122';FriendlyName='Windows PowerShell 5 RTM';ApplicableOS='Windows 10 1511 + KB3140743 1603'}
        $psVersionMappings += New-Object PSObject -Property @{Name='5.0.10586.117';FriendlyName='Windows PowerShell 5 RTM 1602';ApplicableOS='Windows Server 2012 R2, Windows Server 2012, Windows Server 2008 R2 SP1, Windows 8.1, and Windows 7 SP1'}
        $psVersionMappings += New-Object PSObject -Property @{Name='5.0.10586.63';FriendlyName='Windows PowerShell 5 RTM';ApplicableOS='Windows 10 1511 + KB3135173 1602'}
        $psVersionMappings += New-Object PSObject -Property @{Name='5.0.10586.51';FriendlyName='Windows PowerShell 5 RTM 1512';ApplicableOS='Windows Server 2012 R2, Windows Server 2012, Windows Server 2008 R2 SP1, Windows 8.1, and Windows 7 SP1'}
        $psVersionMappings += New-Object PSObject -Property @{Name='5.0.10514.6';FriendlyName='Windows PowerShell 5 Production Preview 1508';ApplicableOS='Windows Server 2012 R2'}
        $psVersionMappings += New-Object PSObject -Property @{Name='5.0.10018.0';FriendlyName='Windows PowerShell 5 Preview 1502';ApplicableOS='Windows Server 2012 R2'}
        $psVersionMappings += New-Object PSObject -Property @{Name='5.0.9883.0';FriendlyName='Windows PowerShell 5 Preview November 2014';ApplicableOS='Windows Server 2012 R2, Windows Server 2012, Windows 8.1'}
        $psVersionMappings += New-Object PSObject -Property @{Name='4.0';FriendlyName='Windows PowerShell 4 RTM';ApplicableOS='Windows Server 2012 R2, Windows Server 2012, Windows Server 2008 R2 SP1, Windows 8.1, and Windows 7 SP1'}
        $psVersionMappings += New-Object PSObject -Property @{Name='3.0';FriendlyName='Windows PowerShell 3 RTM';ApplicableOS='Windows Server 2012, Windows Server 2008 R2 SP1, Windows 8, and Windows 7 SP1'}
        $psVersionMappings += New-Object PSObject -Property @{Name='2.0';FriendlyName='Windows PowerShell 2 RTM';ApplicableOS='Windows Server 2008 R2 SP1 and Windows 7'}
        foreach ($psVersionMapping in $psVersionMappings)
        {
            If ($psVersion -ge $psVersionMapping.Name) {
                @{CurrentVersion=$psVersion;FriendlyName=$psVersionMapping.FriendlyName;ApplicableOS=$psVersionMapping.ApplicableOS}
                Break
            }
        }
    }
    Else{
        @{CurrentVersion='1.0';FriendlyName='Windows PowerShell 1 RTM';ApplicableOS='Windows Server 2008, Windows Server 2003, Windows Vista, Windows XP'}
    }
    

    You can download the detailed script from How to determine installed PowerShell version.

    0 讨论(0)
  • 2020-11-22 10:02

    You can look at the built in variable, $psversiontable. If it doesn't exist, you have V1. If it does exist, it will give you all the info you need.

    1 >  $psversiontable
    
    Name                           Value                                           
    ----                           -----                                           
    CLRVersion                     2.0.50727.4927                                  
    BuildVersion                   6.1.7600.16385                                  
    PSVersion                      2.0                                             
    WSManStackVersion              2.0                                             
    PSCompatibleVersions           {1.0, 2.0}                                      
    SerializationVersion           1.1.0.1                                         
    PSRemotingProtocolVersion      2.1    
    
    0 讨论(0)
  • 2020-11-22 10:03

    You can also call the "host" command from the PowerShell commandline. It should give you the value of the $host variable.

    0 讨论(0)
  • 2020-11-22 10:06

    I would use either Get-Host or $PSVersionTable. As Andy Schneider points out, $PSVersionTable doesn't work in version 1; it was introduced in version 2.

    get-host
    
    Name             : ConsoleHost
    Version          : 2.0
    InstanceId       : d730016e-2875-4b57-9cd6-d32c8b71e18a
    UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
    CurrentCulture   : en-GB
    CurrentUICulture : en-US
    PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
    IsRunspacePushed : False
    Runspace         : System.Management.Automation.Runspaces.LocalRunspace
    
    $PSVersionTable
    
    Name                           Value
    ----                           -----
    CLRVersion                     2.0.50727.4200
    BuildVersion                   6.0.6002.18111
    PSVersion                      2.0
    WSManStackVersion              2.0
    PSCompatibleVersions           {1.0, 2.0}
    SerializationVersion           1.1.0.1
    PSRemotingProtocolVersion      2.1
    
    0 讨论(0)
  • 2020-11-22 10:06

    To check if PowerShell is installed use:

    HKLM\Software\Microsoft\PowerShell\1 Install ( = 1 )
    

    To check if RC2 or RTM is installed use:

    HKLM\Software\Microsoft\PowerShell\1 PID (=89393-100-0001260-00301) -- For RC2
    HKLM\Software\Microsoft\PowerShell\1 PID (=89393-100-0001260-04309) -- For RTM
    

    Source: this website.

    0 讨论(0)
  • 2020-11-22 10:06

    Since the most helpful answer didn't address the if exists portion, I thought I'd give one take on it via a quick-and-dirty solution. It relies on PowerShell being in the path environment variable which is likely what you want. (Hat tip to the top answer as I didn't know that.) Paste this into a text file and name it

    Test Powershell Version.cmd

    or similar.

    @echo off
    echo Checking powershell version...
    del "%temp%\PSVers.txt" 2>nul
    powershell -command "[string]$PSVersionTable.PSVersion.Major +'.'+ [string]$PSVersionTable.PSVersion.Minor | Out-File ([string](cat env:\temp) + '\PSVers.txt')" 2>nul
    if errorlevel 1 (
     echo Powershell is not installed. Please install it from download.Microsoft.com; thanks.
    ) else (
     echo You have installed Powershell version:
     type "%temp%\PSVers.txt"
     del "%temp%\PSVers.txt" 2>nul
    )
    timeout 15
    
    0 讨论(0)
提交回复
热议问题