小智 3463
使用$PSVersionTable.PSVersion以确定发动机的版本.如果变量不存在,则可以安全地假设引擎是版本1.0.
请注意$Host.Version并且(Get-Host).Version不可靠 - 它们仅反映主机的版本,而不是引擎.PowerGUI,PowerShellPLUS等都是托管应用程序,他们会设置主机的版本以反映他们的产品版本 - 这是完全正确的,但不是你想要的.
PS C:\> $PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
4 0 -1 -1
Run Code Online (Sandbox Code Playgroud)
Tho*_*att 426
我会使用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
Run Code Online (Sandbox Code Playgroud)
Mag*_*ndi 95
要确定是否已安装PowerShell,您可以检查注册表是否存在
HKEY_LOCAL_MACHINE\Software\Microsoft\PowerShell\1\Install
Run Code Online (Sandbox Code Playgroud)
和
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\3
Run Code Online (Sandbox Code Playgroud)
如果存在,则值是否为1(已安装),详见博客文章检查PowerShell是否已安装和版本.
要确定已安装的PowerShell的版本,可以检查注册表项
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine\PowerShellVersion
Run Code Online (Sandbox Code Playgroud)
和
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\3\PowerShellEngine\PowerShellVersion
Run Code Online (Sandbox Code Playgroud)
要确定从.ps1脚本安装的PowerShell版本,可以使用以下单行,如PowerShell.com上详细介绍的PowerShell版本运行.
$isV2 = test-path variable:\psversiontable
Run Code Online (Sandbox Code Playgroud)
同一站点还提供了返回版本的功能:
function Get-PSVersion {
if (test-path variable:psversiontable) {$psversiontable.psversion} else {[version]"1.0.0.0"}
}
Run Code Online (Sandbox Code Playgroud)
And*_*der 94
你可以看一下内置变量$psversiontable.如果它不存在,则为V1.如果确实存在,它将为您提供所需的所有信息.
1 > $psversiontable
Name Value
---- -----
CLRVersion 2.0.50727.4927
BuildVersion 6.1.7600.16385
PSVersion 2.0
WSManStackVersion 2.0
PSCompatibleVersions {1.0, 2.0}
SerializationVersion 1.1.0.1
PSRemotingProtocolVersion 2.1
Run Code Online (Sandbox Code Playgroud)
Pat*_*ard 42
只想在这里加2美分.
只能通过外部调用PowerShell直接检查版本,例如从命令提示符
powershell -Command "$PSVersionTable.PSVersion"
Run Code Online (Sandbox Code Playgroud)
编辑:
根据@psaul你实际上可以有一个与它来自哪里无关的命令(CMD,Powershell或Pwsh),谢谢你.
powershell -command "(Get-Variable PSVersionTable -ValueOnly).PSVersion"
Run Code Online (Sandbox Code Playgroud)
我已经测试过,它在CMD和Powershell上都运行良好
Ecl*_*ses 34
您可以通过完成以下检查来验证是否已安装Windows PowerShell版本:
在Windows PowerShell控制台中,在命令提示符处键入以下命令,然后按Enter:
获取主机| 选择对象版本
您将看到如下所示的输出:
Get-Host | Select-Object Version
Run Code Online (Sandbox Code Playgroud)
http://www.myerrorsandmysolutions.com/how-to-verify-the-windows-powershell-version-installed/
Sta*_*ish 21
Microsoft建议的前向兼容方法用于检查PowerShell是否已安装,并确定已安装的版本是查看两个特定的注册表项.我已经在这里重现了细节,以防链接中断.
根据链接页面:
取决于任何其他注册表项,或PowerShell.exe的版本或PowerShell.exe的位置不能保证长期工作.
要检查是否安装了任何版本的PowerShell,请在注册表中检查以下值:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1要检查是否安装了版本1.0或2.0的PowerShell,请在注册表中检查以下值:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\PowerShellEngineSha*_*rpC 11
我找到了检查安装是否最简单的方法:
cmd,然后确定)powershell然后回车.然后,您应该获得PowerShell PS提示:C:\Users\MyUser>powershell
Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.
PS C:\Users\MyUser>
Run Code Online (Sandbox Code Playgroud)
然后,您可以通过键入$PSVersionTable.PSVersion以下内容从PowerShell提示中检查版本:
PS C:\Users\MyUser> $PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
2 0 -1 -1
PS C:\Users\MyUser>
Run Code Online (Sandbox Code Playgroud)
类型exit,如果你想(回到命令提示符下exit,如果你也想关闭命令提示符再次).
要运行脚本,请访问http://ss64.com/ps/syntax-run.html.
$host.version是完全错误/不可靠的.这将为您提供托管可执行文件的版本(powershell.exe,powergui.exe,powershell_ise.exe,powershellplus.exe等),而不是引擎本身的版本.
引擎版本包含在$psversiontable.psversion.对于PowerShell 1.0,此变量不存在,因此很明显,如果此变量不可用,则显然假设引擎为1.0是完全安全的.
小智 9
使用:
$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'}
}
Run Code Online (Sandbox Code Playgroud)
您可以从如何确定已安装的PowerShell版本下载详细脚本.
要检查PowerShell是否已安装,请使用:
HKLM\Software\Microsoft\PowerShell\1 Install ( = 1 )
Run Code Online (Sandbox Code Playgroud)
要检查是否安装了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
Run Code Online (Sandbox Code Playgroud)
资料来源:本网站.
由于最有帮助的答案没有解决if存在部分,我想我会通过一个快速而肮脏的解决方案来解决它.它依赖于PowerShell在路径环境变量中,这可能是你想要的.(帽子提示顶部答案,因为我不知道.)将其粘贴到文本文件中并命名
Test Powershell Version.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
Run Code Online (Sandbox Code Playgroud)
忘记这个页面并且永远不会回到它的最简单的方法是学习Get-Variable:
Get-Variable | where {$_.Name -Like '*version*'} | %{$_[0].Value}
Run Code Online (Sandbox Code Playgroud)
没有必要记住每个变量.这就Get-Variable足够了("应该有关于版本的东西").
我需要检查 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"
}
Run Code Online (Sandbox Code Playgroud)
小智 6
接受的答案仅适用于在计算机上安装了一个版本的 PowerShell。随着 PowerShell 7 的出现,这种情况变得越来越不可能。
Microsoft 的文档指出,安装 PowerShell 7 时会创建其他注册表项:
从 PowerShell 7.1 开始,[installer] 包创建注册表项,用于存储 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
Run Code Online (Sandbox Code Playgroud)
可以看到,我电脑上安装的PowerShell 7版本是7.1.3。如果目标计算机上未安装PowerShell 7 ,则整个密钥不应存在。
如 Microsoft 文档中所述,注册表路径会因安装的 PowerShell 版本而略有不同。
在某些情况下,部分关键路径更改可能会带来挑战,但对于那些对基于命令行的解决方案感兴趣的人,PowerShell 本身可以轻松处理此问题。
用于查询此注册表值中的数据的 PowerShell cmdlet 是Get-ItemPropertyValuecmdlet。观察它的使用和输出如下(注意星号通配符用来代替可能改变的关键路径部分):
PS> Get-ItemPropertyValue -Path "HKLM:\SOFTWARE\Microsoft\PowerShellCore\InstalledVersions\*" -Name "SemanticVersion"
7.1.3
Run Code Online (Sandbox Code Playgroud)
只是一个简单的单线。
以下 cmdlet 将返回 PowerShell 版本。
$PSVersionTable.PSVersion.Major
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2858161 次 |
| 最近记录: |