- AccessChk: View effective permissions on files and folders - Thu, Apr 13 2023
- Read NTFS permissions: View read, write, and deny access information with AccessEnum - Wed, Mar 29 2023
- Kill Windows a process with Tskill and Taskkill - Mon, Mar 13 2023
Checking the PowerShell edition
In general, we differentiate between two PowerShell editions:
- 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.
- 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.
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:
- Get-Host cmdlet
- $Host variable
- Registry
- $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.
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
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.
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
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:
- Check the registry for the Desktop edition version
- Check the registry for the Core edition version (different key)
- 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
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
Checking the PowerShell version on a remote host
This method also works well to check the PowerShell version remotely using Invoke-Command.
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.
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 }
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.
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!
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.
You said you would discuss changes in version 7 and 7.2, but I didn’t find that in the article.
That wast my mistake. The methods just also apply to the latest PowerShell versions. We changed the wording.Thanks for the hint!