我如何确定计算机上安装了哪种版本的PowerShell,以及是否确实安装了PowerShell?
当前回答
这是“批处理文件获取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版本在批处理文件中执行条件流
通用示例
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
)
以下cmdlet将返回PowerShell版本。
$PSVersionTable.PSVersion.Major
只能通过从外部调用PowerShell(例如从命令提示符),使用一行代码直接检查版本
powershell -Command "$PSVersionTable.PSVersion"
根据@psaul的说法,您实际上可以有一个不知道来自何处的命令(CMD、PowerShell或Pwsh)。谢谢你。
powershell -command "(Get-Variable PSVersionTable -ValueOnly).PSVersion"
我已经测试过了,它在CMD和PowerShell上都运行得很完美。
$host.version完全错误/不可靠。这将提供宿主可执行文件的版本(powershell.exe、powergui.exe、powershell_ise.exe、powershell plus.exe等),而不是引擎本身的版本。
引擎版本包含在$psversiontable.psversion中。对于PowerShell 1.0,此变量不存在,因此显然,如果此变量不可用,则假设引擎为1.0是完全安全的。
您可以通过完成以下检查来验证是否安装了Windows PowerShell版本:
单击开始,单击所有程序,单击附件,单击Windows PowerShell,然后单击Windows PowerShell。在Windows PowerShell控制台中,在命令提示符处键入以下命令,然后按ENTER键:获取主机|选择对象版本
您将看到如下输出:
Version
-------
3.0
http://www.myerrorsandmysolutions.com/how-to-verify-the-windows-powershell-version-installed/
推荐文章
- Maven检查存储库中更新的依赖项
- 如何在PowerShell中获得本地主机名?
- PowerShell:如何将数组对象转换为PowerShell中的字符串?
- 从PowerShell ISE中的另一个PS1脚本调用PowerShell脚本PS1
- 如何运行一个PowerShell脚本而不显示窗口?
- PowerShell:仅为单个命令设置环境变量
- 是否有一种方法可以通过双击.ps1文件来使PowerShell脚本工作?
- Node -使用NODE_MODULE_VERSION 51根据不同的Node.js版本编译
- PowerShell等价于grep -f
- “Write-Host”,“Write-Output”,或“[console]::WriteLine”之间的区别是什么?
- Powershell相当于bash的&号(&),用于分叉/运行后台进程
- PowerShell脚本在机器上返回。net框架的版本?
- 如何在PowerShell中获得MD5校验和
- 如何在PowerShell格式化日期时间
- PowerShell和-contains操作符