我如何确定计算机上安装了哪种版本的PowerShell,以及是否确实安装了PowerShell?


当前回答

我需要检查PowerShell的版本,然后运行相应的代码。我们的一些服务器运行v5,其他服务器运行v4。这意味着某些功能(如压缩)可能可用,也可能不可用。

这是我的解决方案:

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"
}

其他回答

我需要检查PowerShell的版本,然后运行相应的代码。我们的一些服务器运行v5,其他服务器运行v4。这意味着某些功能(如压缩)可能可用,也可能不可用。

这是我的解决方案:

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"
}

只能通过从外部调用PowerShell(例如从命令提示符),使用一行代码直接检查版本

powershell -Command "$PSVersionTable.PSVersion"

根据@psaul的说法,您实际上可以有一个不知道来自何处的命令(CMD、PowerShell或Pwsh)。谢谢你。

powershell -command "(Get-Variable PSVersionTable -ValueOnly).PSVersion"

我已经测试过了,它在CMD和PowerShell上都运行得很完美。

这是一个很老的问题,但仍然相关,只是问题的性质在2023年有所不同。找到版本很容易,但首先我们必须启动正确的可执行文件。为此,我们基本上回到了注册表中。

reg query "HKLM\SOFTWARE\Microsoft\PowerShell\1" /v Install >nul 2>&1
if %ERRORLEVEL% EQU 0 (
  :: Default to PowerShell 5 if both are installed
  set PSEXE=powershell
) else (
  set PSEXE=pwsh
)
echo Using %PSEXE%
%PSEXE% -ExecutionPolicy bypass -command "& { ... ; exit $LASTEXITCODE }"

通过检查环境变量可以获得其他提示,但我认为测试注册表中的“Windows”PowerShell是最安全的。

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'}
}

您可以从How to determined installed PowerShell version下载详细脚本。

PowerShell 7

只有在计算机上安装了一个版本的PowerShell时,接受的答案才适用。随着PowerShell 7的出现,这种情况变得越来越不可能。

Microsoft的文档指出,安装PowerShell 7时会创建其他注册表项:

从PowerShell 7.1开始,[安装程序]包创建注册表项存储PowerShell安装位置和版本的。这些值位于HKLM\Software\Microsoft\PowerShellCore\InstalledVersions\<GUID>。这个<GUID>的值对于每个生成类型(发布或预览)都是唯一的,主要版本和架构。

在前面提到的位置搜索注册表会发现以下注册表值:SemanticVersion。该值包含我们所寻求的信息。

在我的计算机上,显示如下:

Path                                                                                           Name              Type Data
----                                                                                           ----              ---- ----
HKLM:\SOFTWARE\Microsoft\PowerShellCore\InstalledVersions\31ab5147-9a97-4452-8443-d9709f0516e1 SemanticVersion String 7.1.3

如您所见,我电脑上安装的PowerShell 7版本为7.1.3。如果目标计算机上未安装PowerShell 7,则整个密钥不应存在。

如Microsoft文档中所述,注册表路径将根据安装的PowerShell版本略有不同。

在某些情况下,部分关键路径更改可能会带来挑战,但对于那些对基于命令行的解决方案感兴趣的人来说,PowerShell本身可以轻松处理此问题。

用于查询此注册表值中的数据的PowerShell cmdlet是Get-ItemPropertyValue cmdlet。如下所示观察其使用和输出(注意使用星号通配符代替可能更改的关键路径部分):

PS> Get-ItemPropertyValue -Path "HKLM:\SOFTWARE\Microsoft\PowerShellCore\InstalledVersions\*" -Name "SemanticVersion"

7.1.3

只是一个简单的单线。