Sometimes it's necessary to find out which PowerShell version you are working with. This is also important for code portability as different PowerShell versions have different capabilities.
The most reliable way to do this is using $PSVersionTable as shown below:
PS C:\> $PSVersionTable
Name Value
---- -----
CLRVersion 2.0.50727.5485
BuildVersion 6.1.7601.17514
PSVersion 2.0
WSManStackVersion 2.0
PSCompatibleVersions {1.0, 2.0}
SerializationVersion 1.1.0.1
PSRemotingProtocolVersion 2.1
--------
To isolate the PSVersion along with any build / revision numbers:
PS C:\> $PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
2 0 -1 -1
or
PS C:\> $PSVersionTable.PSVersion.ToString()
2.0
--------
Here is something quite confusing that may throw people off also, and that's
the path to Powershell which you may see:
C:\Windows\System32\WindowsPowerShell\v1.0>
To the casual observer this would likely lead you to believe that you have PowerShell v1.0 installed. This is likely NOT the case, and can be easily checked by doing the same process here (PSVersionTable was first introduced into Powershell version 2.0) :
C:\Windows\System32\WindowsPowerShell\v1.0>.\powershell.exe
Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.
PS C:\Windows\System32\WindowsPowerShell\v1.0> $PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
2 0 -1 -1
--------
Pretty misleading huh? For some reason the v1.0 directory is retained, but the version of Powershell is NOT v1.0 but v2.0
Also note that the Powershell Get-Host command will return the following:
PS C:\Windows\System32\WindowsPowerShell\v1.0> Get-Host
Name : ConsoleHost
Version : 2.0
InstanceId : 36ff6f4c-5d3e-4ce0-9d47-3d8c4d2b5ae3
UI : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture : en-AU
CurrentUICulture : en-US
PrivateData : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
IsRunspacePushed : False
Runspace : System.Management.Automation.Runspaces.LocalRunspace
--------
When you see this, is it is not actually your Powershell version, but the version of the host that we use to connect to the Powershell engine. In this case ConsoleHost is our host. The major/minor version numbers do match in this instance, but that is not necessarily the case!
There is some code from here, that will allow you to determine your version, even on Powershell version 1.0, by a simple process of logical elimination as PS v1.0 does NOT have a PSversionTable defined at all.
http://powershell.com/cs/media/p/2617.aspx
# If the $PSVersionTable variable doesn't exist, then you are running V1.
# If it does exist, then the version will be available as $PSVersionTable.PSVersion.
function Get-PSVersion {
if (test-path variable:psversiontable) {$psversiontable.psversion} else {[version]"1.0.0.0"}
}
Then call the defined function:
PS C:\Windows\System32\WindowsPowerShell\v1.0> Get-PSVersion
Major Minor Build Revision
----- ----- ----- --------
2 0 -1 -1
--------
Therefore this is not v.1.0 of Powershell but v2.0.
Comments