PowerShell’s Docs General PowerShell Docs How to check the PowerShell version

It is important for an administrator to know what Windows PowerShell and PowerShell edition and version are installed on a system, especially due to script compatibility. This article covers all the ways to check the PowerShell version on Windows, Linux, and MacOS and offers tips and tricks. The methods discussed here also work with PowerShell 7, 7.1 and 7.2

Checking the PowerShell edition ^

In general, we differentiate between two PowerShell editions:

  1. Desktop—Formerly Windows PowerShell edition, available only in Windows. Its version numbers range from 1.0 to 5.1, which is still the default version in Windows Server 2019/2022 and Windows 10/11. It is part of the operating system.
  2. Core—Cross-platform PowerShell, formerly known as PowerShell Core, is available in Windows, Linux, and MacOS. Its version numbers start at 6 (PowerShell Core); the current version is 7.2.3, known as PowerShell.

The easiest way to check your PowerShell edition is to use an automatic variable, $PSEdition.

Checking the PowerShell edition using the $PSEdition variable

Checking the PowerShell edition using the $PSEdition variable

I will use the edition names throughout the post for clarity.

While it is relatively easy to get the correct version in Linux and MacOS, where you can only have the PowerShell edition, it might be a bit trickier in Windows, where you can have both the Desktop and Core editions.

Checking the PowerShell version in Windows ^

In Windows, there are four methods for getting the correct version of PowerShell:

  1. Get-Host cmdlet
  1. $Host variable
  2. Registry
  3. $PSVersionTable variable

Some of them are less reliable than others; some do not work properly when called remotely. Before moving forward with them, let's spend a moment discussing Windows PowerShell vs. PowerShell.

Windows PowerShell vs. PowerShell

As discussed above, Windows is the only system where you can install both PowerShell editions. So, which of them will you query? Will the query return a version of both? This might be a bit misleading, especially for less experienced administrators.

The answer is simple. Windows PowerShell and PowerShell have different and independent executables (and icons). For Windows PowerShell, it is powershell.exe; for PowerShell, it is pwsh.exe.

So, locally, this is easy—it depends on which PowerShell you start and run the query in. Remotely, it is a different story, as you will see below.

Windows PowerShell vs. PowerShell version

Windows PowerShell vs. PowerShell version

Get-Host cmdlet

In PowerShell, a host is a program hosting the PowerShell engine. Visual Studio Code with an integrated terminal is an example of a host. The Get-Host cmdlet returns information about the host, including the version. To display only PowerShell, you can run this command:

(Get-Host).version
Using the Get Host cmdlet

Using the Get Host cmdlet

However, the cmdlet returns the version of the host you are running, not the PowerShell engine itself. These might be different. Also, this cmdlet does not work remotely—it always returns version 1.0.0.0, even on Windows Server 2022.

$Host variable

The $Host variable is an automatic variable, which is the same object that Get-Host returns. As the methods are interchangeable, they might not return the correct engine version, and they don't work remotely. I do not recommend using these at all. To display the contents of the $Host variable, you can just enter it on the PowerShell console.

Using the $Host variable

Using the $Host variable

Registry

Another option is to use the registry. There are three basic ways to get such information—graphically with regedit, command line with reg query, and PowerShell. As this post is all about PowerShell, let's look at that method.

[version](Get-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\PowerShell\3\PowerShellEngine -Name 'PowerShellVersion').PowerShellVersion
You can also use this method remotely with Invoke-Command.
$script = { [version](Get-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\PowerShell\3\PowerShellEngine -Name 'PowerShellVersion').PowerShellVersion }
Invoke-Command -ComputerName srv2022 -ScriptBlock $script
Using registry method locally and remotely

Using registry method locally and remotely

Watch out—running such a command will only return the Desktop edition version. However, new registry keys were added in PowerShell 7.1, and we can now also query the registry for Core installation. The following code will:

  1. Check the registry for the Desktop edition version
  2. Check the registry for the Core edition version (different key)
  3. If it doesn't find the Core registry key, a warning message is shown
$script = { [version](Get-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\PowerShell\3\PowerShellEngine -Name 'PowerShellVersion').PowerShellVersion
try {
[version] (Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\PowerShellCore\InstalledVersions -ErrorAction stop | Get-ItemProperty -Name SemanticVersion ).SemanticVersion
 } catch {
 Write-Host "PowerShell 7.1 or higher is not installed, can't query the registry for previous PowerShell Core versions." -ForegroundColor Yellow
 }
}
Invoke-Command -ComputerName srv2019 -ScriptBlock $script
Querying the remote system using the registry method for Desktop and Core editions

Querying the remote system using the registry method for Desktop and Core editions

Although this method is absolutely reliable regarding the returned version, its syntax is quite long for daily usage. Luckily, one other method is available.

$PSVersionTable variable

Last, we'll look at what is probably the most useful method, another automatic variable called $PSVersionTable. This is a very reliable method that gives you not only the correct PowerShell engine version but also its edition (Desktop or Core). Run this command to expand the version property.

$PSVersionTable.PSversion
Using the $PSVersionTable variable

Using the $PSVersionTable variable

Checking the PowerShell version on a remote host

This method also works well to check the PowerShell version remotely using Invoke-Command.

Querying a remote computer with $PSVersionTable

Querying a remote computer with $PSVersionTable

Watch out—In the screen capture, you can see that I have executed the command in PowerShell 7, but the result I got was version 5.1, which is the Desktop edition.

This happens because, by default, Invoke-Command always connects to the Windows PowerShell remoting endpoint unless otherwise specified. If I want to know what Core version I have installed on that server, I have to add the -ConfigurationName parameter to the command.

Querying the Core edition version with $PSVersionTable

Querying the Core edition version with $PSVersionTable

Below, a little code will query the remote system for all possible versions and editions.

Invoke-Command -ComputerName srv2019 -ScriptBlock {$PSVersionTable.PSVersion}
try {
    Invoke-Command -ComputerName srv2019 -ConfigurationName "powershell.6" -ScriptBlock {$PSVersionTable.PSVersion} -ErrorAction stop
} catch { 
    Write-Host "PowerShell 6 is not installed" -ForegroundColor Yellow
}
try {  
    Invoke-Command -ComputerName srv2019 -ConfigurationName "powershell.7" -ScriptBlock {$PSVersionTable.PSVersion} -ErrorAction stop
} catch {  
    Write-Host "PowerShell 7 is not installed" -ForegroundColor Yellow
}
Retrieving all installed versions

Retrieving all installed versions

This method is by far the most accurate and easiest to use.

Checking the PowerShell version on Linux ^

Linux only supports the Core edition of PowerShell. In addition, updating the PowerShell installation is usually an in-place upgrade, so in the end, you can have only one PowerShell version installed.

As Linux does not have registry, there are three methods we can use—Get-Host, the $host variable, and the $PSVersionTable variable.

Getting the PowerShell version in Linux

Getting the PowerShell version in Linux

All of these methods work in Linux. Regarding the Get-Host method, I would expect the same behavior in Linux as in Windows, especially remotely, but I haven't tested it.

Checking the PowerShell version on macOS ^

MacOS also only supports the Core edition of PowerShell. Just as in Linux, the new version is usually an in-place upgrade.

The methods in MacOS are also the same as in Linux—Get-Host, the $host variable, and the $PSVersionTable variable.

Subscribe to 4sysops newsletter!

Getting the PowerShell version in MacOS

Getting the PowerShell version in MacOS

Final words ^

This post gives you a comprehensive guide on how to check the installed PowerShell version and edition on Windows, Linux, and macOS. You have also learned how to distinguish between the two editions and how to check the information remotely.

Discussion (2)

Leave a Reply

Your email address will not be published.

© 4sysops 2006 - 2022

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account