如何检查本地和远程系统中安装的 PowerShell 版本?

powershellmicrosoft technologiessoftware & coding更新于 2026/2/13 1:22:17

要检查系统中安装的 PowerShell 版本,您可以使用 $PSVersionTable 或 $host 命令。

  • 检查远程服务器中是否可以使用 $host 命令。

在系统中打开 PowerShell 控制台并运行命令 $PSVersionTable。

$PSVersionTable

输出

PS C:\WINDOWS\system32> $PSVersionTable
Name                             Value
----                             -----
PSVersion                        5.1.18362.628
PSEdition                        Desktop
PSCompatibleVersions             {1.0, 2.0, 3.0, 4.0...}
BuildVersion                     10.0.18362.628
CLRVersion                       4.0.30319.42000
WSManStackVersion                3.0
PSRemotingProtocolVersion        2.3
SerializationVersion             1.1.0.1

这里我们得到了 $PSVersionTable 的输出。您可以看到输出属性 $PSVersion,它指示了 PowerShell 的版本信息。

$PSVersionTable.PSVersion

输出

Major    Minor    Build    Revision
-----    -----    -----    --------
5       1         18362    628

Major 属性表示 PowerShell 版本为 5,Build 为 18362。

同样,您可以使用 PowerShell 中的 $Host 命令获得上述输出。

PS C:\WINDOWS\system32> $Host
Name :                       ConsoleHost
Version                      : 5.1.18362.628
InstanceId                   : f6d2bf19-db26-403b-9749-afede37ea56f
UI                           : System.Management.Automation.Internal.Host.InternalHostUserInterface CurrentCulture               :en-IN
CurrentUICulture             : en-US
PrivateData                  : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled              : True
IsRunspacePushed             : False
Runspace                     : System.Management.Automation.Runspaces.LocalRunspace

您可以从 version 属性获取 PowerShell 版本。

$Host.Version

输出

PS C:\WINDOWS\system32> $Host.Version
Major    Minor    Build    Revision
 -----   -----    -----    --------
 5       1        18362    628

要获取远程计算机上的输出,您需要使用 Invoke-Command 或 PSRemote 会话命令,因为 $PSverionTable 和 $Host 不支持减号 ComputerName 参数。

Invoke-Command -ComputerName Test-PC -ScriptBlock{$PSVersionTable.PSVersion}

如果您有多台计算机,并且需要主机名以及与主机名对应的 PS 版本,则可以使用 Pipeline 或 PSCustomObject 命令。

示例

Invoke-Command -ComputerName Test-PC,DC1 -ScriptBlock{$PSVersionTable.PSVersion} |选择 PSComputerName,@{N="PS Version";E={$_.Major}}

如果您有服务器列表,则可以将所有服务器添加到文本文件中并运行上述命令。

例如,我们的服务器列表存储在 D:\Temp\Servers.txt 中,我们需要获取这些服务器的 PS 版本。

Invoke-Command -ComputerName (Get-Content D:\Temp\Servers.txt) -
ScriptBlock{$PSVersionTable.PSVersion} | Select PSComputerName, @{N="PS Version";E={$_.Major}}

相关文章