How to Determine and Check the PowerShell Version

Here we explain how to determine and check the PowerShell version.

Why Check the PowerShell Version?

It’s important to know which version of PowerShell an endpoint is running since some cmdlets are only supported in specific versions of PowerShell. And since PowerShell is updated via Windows Updates, and some machines are typically updated before others, we may need to verify the version to ensure backward-compatibility.

We can do so by running this command:

$PSVersionTable.PSVersion

which will result in something similar to the following:

Name                           Value                                                                                                                                          
----                           -----                                                                                                                                          
PSVersion                      5.1.19041.3031                                                                                                                                 
PSEdition                      Desktop                                                                                                                                        
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                                        
BuildVersion                   10.0.19041.3031                                                                                                                                
CLRVersion                     4.0.30319.42000                                                                                                                                
WSManStackVersion              3.0                                                                                                                                            
PSRemotingProtocolVersion      2.3                                                                                                                                            
SerializationVersion           1.1.0.1   

We can drill down into this example and extract the specific parts of the PowerShell version – namely major, minor, build and revision – like so:

$PSVersionTable.PSVersion.Major
$PSVersionTable.PSVersion.Minor
$PSVersionTable.PSVersion.Build
$PSVersionTable.PSVersion.Revision

And we can compare version numbers with PowerShell simplistically like so:

$currentVersion = $PSVersionTable.PSVersion
$versionToCheck = [version]"5.1.19041.3031"
switch (($currentVersion).CompareTo($versionToCheck)) {
{$_ -lt 0} { write-host "$versionToCheck is greater than the current version of $currentVersion" }
{$_ -gt 0} { write-host "$versionToCheck is less than the current version of $currentVersion" }
{$_ -eq 0} { write-host "$versionToCheck is the same as the current version of $currentVersion" }
}

We essentially write a version as a string of text, cast it as a [version] object and then compare it to the [version] object retrieved from $PSVersionTable.PSVersion .

 

 

 

How To Determine the Installed PowerShell Version

Sometimes we need to know how to determine the installed PowerShell version on a machine, since later versions come with additional features and improved performance.

PowerShell 1.0 shipped with 129 cmdlets but PowerShell 2.0 had over 600 cmdlets! And over the years the number of cmdlets has increased, introducing features such as remote management, background jobs, enhanced debugging, network diagnostics and much more!

Beware of unreliable ways to find the PowerShell version such as:

$Host.Version

and

(Get-Host).Version

Both of the aforementioned approaches have been known to return the version of the host running the PowerShell code (PowerGUI etc) as opposed to the engine itself!

How To Determine the Installed PowerShell Version

The easiest way to determine the installed version of the PowerShell engine is to run:

$PSVersionTable.PSVersion

If we wanted to drill down to each specific part we could do the following:

write-host $PSVersionTable.PSVersion.Major
write-host $PSVersionTable.PSVersion.Minor
write-host $PSVersionTable.PSVersion.Build
write-host $PSVersionTable.PSVersion.Revision

or indeed concatenate them into one string:

write-host $("{0}.{1}.{2}.{3}" -f $PSVersionTable.PSVersion.Major, $PSVersionTable.PSVersion.Minor, $PSVersionTable.PSVersion.Build, $PSVersionTable.PSVersion.Revision)

and if you wanted to use a batch file to get the PowerShell version you could use:

powershell -command "write-host $PSVersionTable.PSVersion.Major $PSVersionTable.PSVersion.Minor $PSVersionTable.PSVersion.Build $PSVersionTable.PSVersion.Revision -separator '.'"

You can see that PowerShell presents multiple ways of achieving the same task – even when joining strings! One approach above used the PowerShell -f format operator to join different parts of the version, and the final approach used the PowerShell -separator operator!