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


当前回答

要确定是否安装了PowerShell,可以检查注册表中是否存在

HKEY_LOCAL_MACHINE\Software\Microsoft\PowerShell\1\Install

and

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\3

如果存在,则值是否为1(对于已安装),如博客文章“检查是否已安装PowerShell和版本”中所述。

要确定安装的PowerShell版本,可以检查注册表项

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine\PowerShellVersion

and

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\3\PowerShellEngine\PowerShellVersion

要确定从.ps1脚本安装的PowerShell版本,可以使用以下一行程序,如PowerShell.com中“我正在运行的PowerShell版本”中所述。

$isV2 = test-path variable:\psversiontable

同一站点还提供了一个返回版本的函数:

function Get-PSVersion {
    if (test-path variable:psversiontable) {$psversiontable.psversion} else {[version]"1.0.0.0"}
}

其他回答

我制作了一个小批量脚本,可以确定PowerShell版本:

@echo off
for /f "tokens=2 delims=:" %%a in ('powershell -Command Get-Host ^| findstr /c:Version') do (echo %%a)

这只是使用Get-Host提取PowerShell的版本并搜索字符串version

当找到具有版本的行时,它使用for命令提取版本。在本例中,我们说定界符是一个冒号,然后搜索第一个冒号,这就是我的例子5.1.183627.52。

要确定是否安装了PowerShell,可以检查注册表中是否存在

HKEY_LOCAL_MACHINE\Software\Microsoft\PowerShell\1\Install

and

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\3

如果存在,则值是否为1(对于已安装),如博客文章“检查是否已安装PowerShell和版本”中所述。

要确定安装的PowerShell版本,可以检查注册表项

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine\PowerShellVersion

and

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\3\PowerShellEngine\PowerShellVersion

要确定从.ps1脚本安装的PowerShell版本,可以使用以下一行程序,如PowerShell.com中“我正在运行的PowerShell版本”中所述。

$isV2 = test-path variable:\psversiontable

同一站点还提供了一个返回版本的函数:

function Get-PSVersion {
    if (test-path variable:psversiontable) {$psversiontable.psversion} else {[version]"1.0.0.0"}
}

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>

要检查是否安装了PowerShell,请使用:

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

要检查是否安装了RC2或RTM,请使用:

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

资料来源:本网站。

忘记此页面并永远不返回它的最简单方法是学习Get Variable:

Get-Variable | where {$_.Name -Like '*version*'} | %{$_[0].Value}

没有必要记住每个变量。仅获取变量就足够了(而且“版本应该有一些东西”)。