Monday, June 18, 2012

How to determine your Powershell version

Waiting for the final release of Powershell 3.0 with Windows Server 2012, I wanted to share here a quick hint on how to get to know whether the Powershell version running on your systems is 1.0, 2.0 or 3.0.

First of all, System.Management.Automation is the root namespace for Powershell, and System.Management.Automation.Internal is the namespace that contains the base classes that define members of classes found in other Windows Powershell namespaces.
Inside the namespace there is an object System.Management.Automation.Internal.Host.InternalHostUserInterface.

This object can be retrieved directly through the built-in $host variable (which is dynamic) or there also is a get-host cmdlet that will get this object for you on demand.


If you want to use the cmdlet to retrieve your Powershell version, just run it this way:
(get-host).version | fl *
which is the aliased version of:
(get-host).version | filter-list -property *
The round brackets are there to tell Powershell that I want to retrieve the 'version' properties of the 'host' object after having parsed it.

If I had written it without brackets I would have received an error like this: "The term 'get-host.version' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again."

In case you have Powershell 1.0, the output would look like this, with the first line telling you the Powershell version:
Major         : 1
Minor         : 0
Build         : 0
Revision      : 0
MajorRevision : 0
MinorRevision : 0
Starting from version 2.0 the output should look like the following list:
Major         : 2
Minor         : 0
Build         : -1
Revision      : -1
MajorRevision : -1
MinorRevision : -1
The syntax using the $host variable is pretty similar (no brackets here):
$host.Version | fl *
The output is strictly the same:
Major         : 2
Minor         : 0
Build         : -1
Revision      : -1
MajorRevision : -1
MinorRevision : -1
Starting from Powershell 2.0 there also is a variable $PsVersionTable which is a hast table containing info about your Powershell version:
$PSVersionTable | fl *

Name  : CLRVersion
Key   : CLRVersion
Value : 2.0.50727.4959

Name  : BuildVersion
Key   : BuildVersion
Value : 6.1.7600.16385

Name  : PSVersion
Key   : PSVersion
Value : 2.0

Name  : WSManStackVersion
Key   : WSManStackVersion
Value : 2.0

Name  : PSCompatibleVersions
Key   : PSCompatibleVersions
Value : {1.0, 2.0}

Name  : SerializationVersion
Key   : SerializationVersion
Value : 1.1.0.1

Name  : PSRemotingProtocolVersion
Key   : PSRemotingProtocolVersion
Value : 2.1
If the $PSVersionTable variable doesn't exist, then you are running Powershell 1.0. Easy, right?

If you are running Powershell 3.0 you will get:
$psversiontable

Name                       Value
----                       -----
WSManStackVersion          3.0
PSCompatibleVersions       {1.0, 2.0, 3.0}
SerializationVersion       1.1.0.1
BuildVersion               6.2.8158.0
PSVersion                  3.0
CLRVersion                 4.0.30319.239
PSRemotingProtocolVersion  2.103
Hope this helps.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...