Determine installed PowerShell version

2018-12-31 22:59发布

How can I determine what version of PowerShell is installed on a computer, and indeed if it is installed at all?

标签: powershell
25条回答
情到深处是孤独
2楼-- · 2018-12-31 23:19

$host.version is just plain wrong/unreliable. This gives you the version of the hosting executable (powershell.exe, powergui.exe, powershell_ise.exe, powershellplus.exe etc) and not the version of the engine itself.

The engine version is contained in $psversiontable.psversion. For PowerShell 1.0, this variable does not exist, so obviously if this variable is not available it is entirely safe to assume the engine is 1.0, obviously.

查看更多
步步皆殇っ
3楼-- · 2018-12-31 23:19

get powershell version by $host.Version command.

output:

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      17134  407
查看更多
高级女魔头
4楼-- · 2018-12-31 23:20

I found the easiest way to check if installed was to:

  • run a command prompt (Start, Run, cmd, then OK)
  • type powershell then hit return. You should then get the PowerShell PS prompt:

C:\Users\MyUser>powershell

Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.

PS C:\Users\MyUser>

You can then check the version from the PowerShell prompt by typing $PSVersionTable.PSVersion:

PS C:\Users\MyUser> $PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
2      0      -1     -1

PS C:\Users\MyUser>

Type exit if you want to go back to the command prompt (exit again if you want to also close the command prompt).

To run scripts, see http://ss64.com/ps/syntax-run.html.

查看更多
骚的不知所云
5楼-- · 2018-12-31 23:21

You can look at the built in variable, $psversiontable. If it doesn't exist, you have V1. If it does exist, it will give you all the info you need.

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    
查看更多
爱死公子算了
6楼-- · 2018-12-31 23:21

I needed to check the version of PS and then run the appropriate code. Some of our servers run v5, others v4. This means that some functions, like compress, may or may not be available.

This is my solution:

if ($PSVersionTable.PSVersion.Major -eq 5) {
    #Execute code available in 5, like Compress
    Write-Host "You are running version 5"
}
else {
    #Use a different process
    Write-Host "This is version $PSVersionTable.PSVersion.Major"
}
查看更多
素衣白纱
7楼-- · 2018-12-31 23:21
# Get PowerShell Version
function Global:version
{
      $PSVersionTable.PSVersion
}
查看更多
登录 后发表回答