Determining the PowerShell version is important for scripts as it tells the script the potential features available on the current PowerShell engine. The best way to do this is use the $PsVersionTable variable in the first method. In this example, the PowerShell version is 7.4.5.

Method 1: Use $PSVersionTable Variable

The most reliable and recommended way to determine the PowerShell version is using the built-in variable $PSVersionTable. $PSVersionTable contains the infromation about the PowerShell engine including the version. In the shell type $PSVersionTable and press enter. Note, if the variable doesn’t exist then the version is 1.0.

Output:

Major  Minor  Patch  PreReleaseLabel BuildLabel
-----  -----  -----  --------------- ----------
7      4      5

Version as a String

In PowerShell to concatinate two integers into a string:

Write-Host "PowerShell $($PSVersionTable.PSVersion.Major).$($PSVersionTable.PSVersion.Minor).$($PSVersionTable.PSVersion.Patch)"

Output:

PowerShell 7.4.5

Method 2: Get-Host

Get-Host is an alternative way to get the version. However, it is less reliable as it is the version of the host and not the engine. The PowerShell engine executes the commands, while the PowerShell host provides an interface (usually through a GUI) to the shell. The most common modern Windows based PowerShell host is Windows Terminal. The distinction between engine and host is also important in when you are remoting into the machine. The remote engine / local host will likely different.

(Get-Host).Version to get the version:

Output:

Major  Minor  Build  Revision
-----  -----  -----  --------
7      4      5      -1

Notice that it also has a different description of the version Major, Minor, Build, Revision instead of Major, Minor, Patch PreReleaseLabel, BuildLabel (i.e., Build instead of Patch).

Method 3: Outside of PowerShell

powershell.exe (PowerShell 5.1.x) was renamed to pwsh.exe starting with version PowerShell 6.0. If you need to determine the PowerShell version outside of PowerShell shell (e.g., from cmd.exe) or within PowerShell run:

pwsh --version

Output:

PowerShell 7.4.5

We can also save the result to a variable for further processing:

$psVer = pwsh --version
Write-Host $psVer

Happy coding ✌️