我如何确定计算机上安装了哪种版本的PowerShell,以及是否确实安装了PowerShell?
当前回答
我发现检查是否安装的最简单方法是:
运行命令提示符(“开始”、“运行”、“命令”、“确定”)键入powershell,然后单击return。然后,您应该得到PowerShell PS提示:
C:\Users\MyUser>powershell
Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.
PS C:\Users\MyUser>
然后,可以通过键入$PSVersionTable.PSVersion:
PS C:\Users\MyUser> $PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
2 0 -1 -1
PS C:\Users\MyUser>
如果要返回到命令提示符,请键入exit(如果还要关闭命令提示符,则再次退出)。
要运行脚本,请参见http://ss64.com/ps/syntax-run.html.
其他回答
以下cmdlet将返回PowerShell版本。
$PSVersionTable.PSVersion.Major
由于最有用的答案没有提到“如果存在”部分,我想我应该通过一个快速而肮脏的解决方案来解决这个问题。它依赖于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
$host.version完全错误/不可靠。这将提供宿主可执行文件的版本(powershell.exe、powergui.exe、powershell_ise.exe、powershell plus.exe等),而不是引擎本身的版本。
引擎版本包含在$psversiontable.psversion中。对于PowerShell 1.0,此变量不存在,因此显然,如果此变量不可用,则假设引擎为1.0是完全安全的。
我会使用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
推荐文章
- 从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操作符
- 使用PowerShell删除超过15天的文件
- 数组添加 vs +=
- 在使用“apt-get”安装包之前,我如何检查版本?