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


当前回答

Microsoft建议的用于检查是否安装了PowerShell并确定已安装版本的前向兼容方法是查看两个特定的注册表项。我在这里复制了细节,以防链接断开。

根据链接页面:

根据任何其他注册表项、PowerShell.exe版本或PowerShell.exe的位置,不能保证长期工作。

要检查是否安装了任何版本的PowerShell,请检查注册表中的以下值:

密钥位置:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shell\1值名称:安装值类型:REG_DWORD值数据:0x00000001(1

要检查是否安装了PowerShell版本1.0或2.0,请检查注册表中的以下值:

密钥位置:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shell\1\PowerShellEngine值名称:PowerShellVersion值类型:REG_SZ值数据:<1.0|2.0>

其他回答

我会使用Get Host或$PSVersionTable。正如Andy Schneider所指出的,$PSVersionTable在版本1中不起作用;它在版本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

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

只是一个简单的单线。

由于最有用的答案没有提到“如果存在”部分,我想我应该通过一个快速而肮脏的解决方案来解决这个问题。它依赖于PowerShell位于路径环境变量中,这可能是您想要的。(由于我不知道,所以给顶部的答案打个小提示。)将其粘贴到文本文件中并命名

测试Powershell版本.cmd

或类似。

@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

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(例如从命令提示符),使用一行代码直接检查版本

powershell -Command "$PSVersionTable.PSVersion"

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

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

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