PowerShell version

Discovering the PowerShell version you’re using can be an interesting task.

The automatic variable $PSVersionTable was introduced in PowerShell v2.  On my Windows 10 version 1803 machine for Windows PowerShell I get

PS> $PSVersionTable

Name                           Value
—-                           —–
PSVersion                      5.1.17134.48
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
BuildVersion                   10.0.17134.48
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3

$PSVersionTable is a hashtable so the order in which items are displayed may vary. The contents of the hashtable have changed over time as well.

The important parts of $PsversionTable include:

The version of PowerShell itself

PS> $PSVersionTable.PSVersion

Major  Minor  Build  Revision
—–  —–  —–  ——–
5      1      17134  48

PS> $PSVersionTable.PSVersion.Major

This a simple test for version.

The edition is also important

PS> $PSVersionTable.PSEdition

Desktop means its full CLR – in other words Windows PowerShell

The WSMAN version is also important

PS> $PSVersionTable.WSManStackVersion

Major  Minor  Build  Revision
—–  —–  —–  ——–
3      0      -1     -1

You need v3 to use CIM sessions.

With PowerShell v6 you get a few more options

PS>  $PSVersionTable

Name                           Value
—-                           —–
PSVersion                      6.0.1
PSEdition                      Core
GitCommitId                    v6.0.1
OS                             Microsoft Windows 10.0.17134
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
WSManStackVersion              3.0

Notice PSEdition is set to Core.

The OS and Platform data are v6 only

The PSRemotingProtocolVersion, SerializationVersion and WSManStackVersion are the same for Windows PowerShell v5.1 and PowerShell v6.

PowerShell v6 runs on Windows, Linux and macOS. You can test which OS you’re on from $PSVersionTable or more simply using the following automatic variables:

PS>  ls variable:\is*

Name                           Value
—-                           —–
IsLinux                        False
IsMacOS                        False
IsWindows                      True
IsCoreCLR                      True

Using these you could create branching logic to perform a task by calling the appropriate command based on the underlying operating system.

This entry was posted in PowerShell v5, PowerShell v6. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s