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
.