更高的支持 )否则( 当前powershell版本不支持回显功能。 )
真实世界示例更高的支持 )否则( 当前powershell版本不支持回显功能。 ),powershell,Powershell,真实世界示例 powershell“退出$PSVersionTable.PSVersion.Major” 如果%errorlevel%GEQ 5( rem自动解压缩归档文件 powershell Expand Archive Compressed.zip )否则( rem使用户解压,因为懒惰 echo请在继续之前解压缩Compressed.zip。。。 暂停 ) 我制作了一个小批量脚本,可以确定PowerShell版本: @echo off for /f "tokens=2 delims=:"
powershell“退出$PSVersionTable.PSVersion.Major”
如果%errorlevel%GEQ 5(
rem自动解压缩归档文件
powershell Expand Archive Compressed.zip
)否则(
rem使用户解压,因为懒惰
echo请在继续之前解压缩Compressed.zip。。。
暂停
)
我制作了一个小批量脚本,可以确定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.18362.752
我在版本7.1.0
上尝试了这一点,它成功了:
$PSVersionTable | Select-Object PSVersion
输出
但是,它在版本5.1
上不起作用,因此在7以下的版本上使用它:
$PSVersionTable.PSVersion
输出
PowerShell 7
仅当计算机上安装了一个版本的PowerShell时,接受的答案才适用。随着PowerShell 7的出现,这种情况变得越来越不可能
Microsoft声明在安装PowerShell 7时会创建其他注册表项:
从PowerShell 7.1开始,[installer]包创建注册表项
存储PowerShell的安装位置和版本的。这些
值位于
HKLM\Software\Microsoft\PowerShellCore\InstalledVersions\
。这个
的值对于每个构建类型(版本或预览)都是唯一的,
主要版本和体系结构
在上述位置浏览注册表将显示以下注册表值: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版本
$PSVersionTable.PSVersion.Major
在某些情况下,部分关键路径更改可能会带来挑战,但对于那些对基于命令行的解决方案感兴趣的人来说,PowerShell本身可以轻松处理此问题
用于查询此注册表值中的数据的PowerShell cmdlet是cmdlet。观察其使用和输出,如下所示(注意用星号字符代替可能更改的键路径部分):
只要简单的一句话。谢谢!注意:在我的XP上,我从v1 Powershell手动升级,实际的文件夹和注册表路径(误导性的?!)引用的是v1而不是v2。这是其他人在这里指定的,但这就是为什么我如此担心是否安装了它的原因。我的道路是;C:\WINDOWS\system32\windowspowershell\v1.0如果$psversiontable不存在,那么假设您在v1.0上是完全安全的-嘿,普雷斯托,这也回答了您的问题<代码>$host.version不可靠-例如,在powergui中,它返回的powergui主机版本与powershell引擎版本不同(这是所需的)。接受的答案表示“…$host.version和(Get-host)。version不可靠-它们只反映主机的版本,而不是引擎的版本”@DanielCassidy-派对推迟了两年,但“某些功能无法获得”可能是因为只安装了.Net 3.5 Compact framework而不是完整的框架。
$PSVersionTable
更可靠,并返回$PSVersion。您还可以使用$PSVersionTable.PSVersion
。即使您远程连接到运行不同版本(invoke命令-computername myRemotePC-Credential foo{$host}
)的计算机,看起来$host
只会显示他们同意用于序列化的最低版本。而$PSVersionTable
将显示真实版本。希望它能帮助某人…似乎$host.Version不是一个好选择。。。如果您远程访问运行PowerShell 3的计算机,则返回1.0,因为远程主机似乎是v1。使用$PSVersionTable正确返回3。@海星显示的命令使用命令提示符启动Powershell。如果没有安装,该命令会不会出错为“powershell not found”(未找到powershell)?我已经厌倦了回到这里,所以我只运行了notepad$profile
,并在其中转储了function psver{$PSVersionTable;$PSVersionTable.PSVersion}
。使用重新加载$配置文件
,我可以随时键入psver
,以获取详细的版本信息和其他详细信息。这很有用,因为在Windows 2003服务器上,我找不到PowerShell的“开始”菜单项(它在那里,但不明显),但注册表项回答了我的问题。对于PowerScript 3.0,似乎有一个新项,HKEY\U LOCAL\U MACHINE\SOFTWARE\Microsoft\PowerShell\3(除了HKEY\U LOCAL\U MACHINE\SOFTWARE\Microsoft\PowerShell\1之外)。在本例中,获取版本的正确位置似乎是HKEY\U LOCAL\U MACHINE\SOFTWARE\Microsoft\PowerShell\3\PowerShellEngine\PowerShellVersion(示例值为“3.0”)。对于PowerShell 3.0,似乎有一个新的,HKEY\U LOCAL\U MACHINE\SOFTWARE\Microsoft\PowerShell\3。哪个与查找PS版本相同?键入$PSVersion[选项卡][输入]。完成。不需要记住任何东西:)只有非常熟悉所使用的管道技巧时,这才真正有用。初学者不可能记住所有这些语法比只记住$PSVersionTableGuys、Get变量更容易(如果只需要获得版本所需的一切),piping这里有一个笑话:)我不记得piping syn
function Get-PSVersion {
if (test-path variable:psversiontable) {$psversiontable.psversion} else {[version]"1.0.0.0"}
}
HKLM\Software\Microsoft\PowerShell\1 Install ( = 1 )
HKLM\Software\Microsoft\PowerShell\1 PID (=89393-100-0001260-00301) -- For RC2
HKLM\Software\Microsoft\PowerShell\1 PID (=89393-100-0001260-04309) -- For RTM
PS C:\> $PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
4 0 -1 -1
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
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
Get-Variable | where {$_.Name -Like '*version*'} | %{$_[0].Value}
Get-Host | Select-Object Version
Version
-------
3.0
@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
C:\Users\MyUser>powershell
Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.
PS C:\Users\MyUser>
PS C:\Users\MyUser> $PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
2 0 -1 -1
PS C:\Users\MyUser>
Get-Host | select {$_.Version}
$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'}
}
$PSVersionTable.PSVersion.Major
powershell -Command "$PSVersionTable.PSVersion"
powershell -command "(Get-Variable PSVersionTable -ValueOnly).PSVersion"
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"
}
@echo off
for /f "tokens=2 delims=:" %%a in ('powershell -Command Get-Host ^| findstr /c:Version') do (echo %%a)
$PSVersionTable | Select-Object PSVersion
PSVersion
---------
7.1.0
$PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
5 1 18362 1171
Path Name Type Data
---- ---- ---- ----
HKLM:\SOFTWARE\Microsoft\PowerShellCore\InstalledVersions\31ab5147-9a97-4452-8443-d9709f0516e1 SemanticVersion String 7.1.3
PS> Get-ItemPropertyValue -Path "HKLM:\SOFTWARE\Microsoft\PowerShellCore\InstalledVersions\*" -Name "SemanticVersion"
7.1.3