我如何确定计算机上安装了哪种版本的PowerShell,以及是否确实安装了PowerShell?
当前回答
要检查是否安装了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-Host | select {$_.Version}
这是“批处理文件获取powershell版本”的顶级搜索结果,因此我想提供一个基本示例,说明如何根据powershell版本在批处理文件中执行条件流
通用示例
powershell "exit $PSVersionTable.PSVersion.Major"
if %errorlevel% GEQ 5 (
echo Do some fancy stuff that only powershell v5 or higher supports
) else (
echo Functionality not support by current powershell version.
)
真实世界示例
powershell "exit $PSVersionTable.PSVersion.Major"
if %errorlevel% GEQ 5 (
rem Unzip archive automatically
powershell Expand-Archive Compressed.zip
) else (
rem Make the user unzip, because lazy
echo Please unzip Compressed.zip prior to continuing...
pause
)
由于最有用的答案没有提到“如果存在”部分,我想我应该通过一个快速而肮脏的解决方案来解决这个问题。它依赖于PowerShell位于路径环境变量中,这可能是您想要的。(由于我不知道,所以给顶部的答案打个小提示。)将其粘贴到文本文件中并命名
测试Powershell版本.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
我制作了一个小批量脚本,可以确定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"}
}
推荐文章
- 如何在PowerShell中获得MD5校验和
- 如何在PowerShell格式化日期时间
- PowerShell和-contains操作符
- 使用PowerShell删除超过15天的文件
- 数组添加 vs +=
- 在使用“apt-get”安装包之前,我如何检查版本?
- PowerShell中用户输入的提示符
- 如何在Angular中显示应用版本?
- 如何从字符串执行任意本机命令?
- 如何使用。net 4运行时运行PowerShell ?
- Flutter:升级游戏商店的版本代码
- 在PowerShell中重新加载路径
- 函数在PowerShell中的返回值
- 如何在PowerShell中输出一些东西
- 调用webrequest, POST参数