我如何确定计算机上安装了哪种版本的PowerShell,以及是否确实安装了PowerShell?
当前回答
由于最有用的答案没有提到“如果存在”部分,我想我应该通过一个快速而肮脏的解决方案来解决这个问题。它依赖于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
其他回答
您可以查看内置变量$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
由于最有用的答案没有提到“如果存在”部分,我想我应该通过一个快速而肮脏的解决方案来解决这个问题。它依赖于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版本”的顶级搜索结果,因此我想提供一个基本示例,说明如何根据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
)
使用$PSVersionTable.PSVersion确定引擎版本。如果变量不存在,则可以安全地假设引擎为1.0版本。
请注意,$Host.Version和(Get-Host).Version不可靠-它们反映只有主机的版本,而不是引擎。PowerGUI,PowerShellPLUS等都是托管应用程序,并且他们将设置主机的版本以反映其产品版本-这是完全正确的,但不是你想要的。
PS C:\> $PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
4 0 -1 -1
我在7.1.0版上尝试过,结果成功了:
$PSVersionTable | Select-Object PSVersion
输出
PSVersion
---------
7.1.0
但它在5.1版上不起作用,所以在7版以下的版本上更适用:
$PSVersionTable.PSVersion
输出
Major Minor Build Revision
----- ----- ----- --------
5 1 18362 1171
EDIT
从PowerShell 7.2.5开始,您现在可以执行以下操作:
pwsh -v
Or
pwsh --version
输出
PowerShell 7.2.5
推荐文章
- 如何在PowerShell中获得MD5校验和
- 如何在PowerShell格式化日期时间
- PowerShell和-contains操作符
- 使用PowerShell删除超过15天的文件
- 数组添加 vs +=
- 在使用“apt-get”安装包之前,我如何检查版本?
- PowerShell中用户输入的提示符
- 如何在Angular中显示应用版本?
- 如何从字符串执行任意本机命令?
- 如何使用。net 4运行时运行PowerShell ?
- Flutter:升级游戏商店的版本代码
- 在PowerShell中重新加载路径
- 函数在PowerShell中的返回值
- 如何在PowerShell中输出一些东西
- 调用webrequest, POST参数