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!