当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 如何检查安装的PowerShell版本?

作者:精品下载站 日期:2024-12-14 22:54:52 浏览:14 分类:玩电脑

如何检查安装的PowerShell版本?


在本文中,我们将了解存在哪些 PowerShell 版本,它们之间有什么区别

Windows PowerShell

PowerShell Core

,以及如何检查本地或远程计算机上安装的 PowerShell 版本。

Windows PowerShell 和 PowerShell Core 的历史记录和版本

从 Windows 7 SP1 和 Windows Server 2008 R2 SP1 开始的所有 Windows 版本中默认安装 PowerShell。下表显示了所有 PowerShell 版本的列表:

PowerShell 1.0

可以在 Windows Server 2003 SP1 和 Windows XP 上手动安装

PowerShell 2.0

Windows Server 2008 R2 和 Windows 7

PowerShell 3.0

Windows 8 和 Windows Server 2012

PowerShell 4.0

Windows 8.1 和 Windows Server 2012 R2

PowerShell 5.0

预装在 Windows 10 RTM 上,并通过 Windows Update 自动更新到 5.1

PowerShell 5.1

它内置于 Windows 10(从 Build 1709 开始)和 Windows Server 2016 中

PowerShell Core 6.0 / 6.1

它是下一个跨平台 PowerShell 版本(基于 .NET Core),可以安装在所有受支持的 Windows 版本以及 MacOS、CentOS、RHEL、Debian、Ubuntu、openSUSE 上

PowerShell Core 7.0

2020年3月发布的最新PowerShell版本(其中使用.NET Core 3.1而不是.NET Core 2.x)

您可以在以前的 Windows 版本中手动安装较新的 PowerShell 版本。为此,请下载并安装适当版本的Windows 管理框架(PowerShell 是其中的一部分)。

值得注意的是,近两年微软暂停了经典Windows PowerShell的开发(仅发布了错误修复和安全更新),转而专注于开源跨平台PowerShell Core

Windows PowerShell 和 PowerShell Core 之间有什么区别?

  1. Windows PowerShell 基于.NET Framework(例如,PowerShell 5 需要 .NET Framework v4.5,请确保已安装)。 PowerShell Core基于.Net Core

  2. Windows PowerShell仅适用于Windows操作系统,而PowerShell Core跨平台的,也可以在Linux中运行;

  3. PowerShell Core 并不完全兼容 Windows PowerShell,但是,Microsoft 正在努力改进与早期 PS cmdlet 和脚本的向后兼容性。 (建议在迁移到 PowerShell Core 之前测试旧的 PS1 脚本)。 PowerShell Core 7 提供与 Windows PowerShell 的最高兼容性;

  4. 您无法使用PowerShell ISE编辑器编辑PowerShell Core脚本(但可以使用Visual Studio Code);

  5. 由于 Windows PowerShell 已不再开发,建议您开始迁移到 PowerShell Core。

如何从控制台获取PowerShell版本?

了解计算机上安装的 PowerShell 版本的最简单方法是使用以下命令:

host

检查版本属性值。

以下屏幕截图是在默认安装了 PowerShell 5.1 的 Windows 10 中制作的,就像在 Windows Server 2016 中一样。

[玩转系统] 如何检查安装的PowerShell版本?

或者

$PSVersionTable

您只能获取 PowerShell 版本值:

$PSVersionTable.PSVersion.major

(在此示例中,我们在干净的 Windows Server 2008 R2 中获得了 PSVersion 2.0)

[玩转系统] 如何检查安装的PowerShell版本?

$PSVersionTable 命令可以在不同操作系统的 PowerShell Core 中正确运行。

您还可以通过注册表查找已安装的PowerShell版本。为此,请获取注册表项中 PowerShellVersion 参数的值

HKLM\SOFTWARE\Microsoft\PowerShell\PowerShellEngine

使用 Get-ItemProperty cmdlet:

(Get-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\PowerShell\PowerShellEngine -Name 'PowerShellVersion').PowerShellVersion

[玩转系统] 如何检查安装的PowerShell版本?

上述方法适用于 Windows Server 2012/Windows 8 或更高版本。

在Windows Server 2008 R2/Windows 7中,您可以在另一个注册表项中获取注册表参数的值:

(Get-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine -Name 'PowerShellVersion').PowerShellVersion

[玩转系统] 如何检查安装的PowerShell版本?

要获取已安装的 PowerShell Core 版本,请使用以下命令:

(Get-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\PowerShellCore\InstalledVersions* -Name 'SemanticVersion').SemanticVersion

检查远程计算机上 PowerShell 的版本

要检查远程主机上的 PowerShell 版本,请使用 $PSVersionTable 环境变量的值或直接从注册表获取信息。其他方法可能会返回不正确的数据。

您可以使用 Invoke-Command cmdlet 通过 PowerShell 远程处理在远程计算机上安装 PowerShell 版本:

Invoke-Command -ComputerName mun-dc01 -ScriptBlock {$PSVersionTable.PSVersion} -Credential $cred

[玩转系统] 如何检查安装的PowerShell版本?

Major Minor Build Revision PSComputerName
----- ----- ----- -------- --------------
5 1 14393 3383 mun-dc01

您可以使用以下脚本获取多台计算机上已安装的 PowerShell 版本(远程计算机列表必须指定为纯文本文件):

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

或者,您可以通过 Get-ADComputer 获取域计算机列表并远程检查它们上的 PowerShell 版本:

$adcomputer=(Get-ADComputer -Filter 'operatingsystem -like "*Windows server*" -and enabled -eq "true"' -SearchBase ‘OU=servers,OU=Munich,dc=a-d,dc=com’ ).Name
Invoke-Command-ComputerName $adcomputer -Scriptblock{$PSVersionTable.psversion} -ErrorAction SilentlyContinue

如果您的 PowerShell 脚本使用特定 PS 版本的功能,您可以强制脚本切换到不同的 PowerShell 版本。例如,要在 PowerShell v3 模式下运行控制台,请运行以下命令(必须安装 .Net Framework 3.5):

PowerShell.exe -version 3

如果您运行使用特定 PS 版本的 cmdlet 或功能的脚本或命令,了解您的 PowerShell 版本可能很重要。如果您想在脚本中检测已安装的PowerShell版本并使用基于它的cmdlet,您可以运行以下PS脚本:

$ps_version = $PSVersionTable.PSVersion.major
if ( $ps_version -eq "2” )
{
write "You are using Powershell 2.0"
}
elseif ( $ps_version -eq "5" )
{
write " You are using Powershell 5"
}

在下一篇文章中,我们将了解如何更新 Windows 中的 PowerShell 版本。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯