Check PowerShell Versions: Best Practices

Published:27 July 2019 - 4 min. read

Today’s sponsor is n8n, the AI-native workflow automation tool built for ITOps and DevSecOps. With 100+ templates to get you started quickly and a powerful visual editor, you can automate complex workflows without giving up control. Check it out here.

 

 

 

 

 

In PowerShell, there are a zillion ways to do the same thing (or close to it). In this blog post, you’ll learn every way to check your PowerShell version that you have on local and remote computers. We’ll cover the bad ways and my recommended way.

There are websites that show various ways to check your Powershell version. But none that compiled a comprehensive list of all of them. I decided to change that.

All these ways should work in both Windows PowerShell and PowerShell Core. These methods should also work in Windows PowerShell versions 1.0 all the way up to PowerShell 7.

The ways you can find out a version of PowerShell you’re running are:

  1. The (Get-Host).Version property
  2. The $host.Version property
  3. The registry (Windows PowerShell only)
  4. The $PSVersionTable.PSVersion property

Let’s break down all the ways to find the version of PowerShell from the least to the most recommended way.

Get-Host

PowerShell has a concept known as hosts. A host is a program that is hosting the PowerShell engine. It is not the PowerShell engine itself. The PowerShell console or a code editor with an integrated terminal are PowerShell hosts.

A host can have a version that is completely independent of PowerShell itself. This can be deceiving to many newcomers. Let me show you why.

If you run (Get-Host).Version, you’ll see that it returns a version number that looks like it could be the PowerShell engine version. Looks can be deceiving.

Below I’ve ran Get-Host on Windows PowerShell 5.1 and you can see it comes back with 5.1.17134.858. This looks like a legitimate version.

PS51> (Get-Host).Version
Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      17134  858

However, sometimes when you run Get-Host in an integrated terminal, the version is not the same. Although usually, the host will represent the same version of the engine, it doesn’t necessarily always do that.

Check Powershell Version via Get-Host on Remote Computers

Even though Get-Host seems to return the same version when run on a local computer, it never will on remote computers.

For example, let’s run Get-Host on a remote Windows Server 2016 server via Invoke-Command and see what happens.

PS51> Invoke-Command -ComputerName 10.0.0.5 -ScriptBlock {Get-Host} -Credential $cred

Major  Minor  Build  Revision PSComputerName
-----  -----  -----  -------- --------------
1      0      0      0        10.0.0.5

The last time I checked, it’s not possible to run PowerShell v1 on Windows Server 2016.

Relying on Get-Host is just a bad idea all around.

Check Powershell Version $host.Version command

Referencing $host.Version is another way to check Powershell version. The $host variable is an automatic variable that returns the same output as Get-Host.

Referencing $host.Version
Referencing $host.Version

There’s nothing special about this method. It’s simply the same as running Get-Host.

$host.Version on Remote Computers

You will see the same behavior via PowerShell Remoting with $host.Version as you will running Get-Host.

PS51> Invoke-Command -ComputerName 10.0.0.5 -ScriptBlock {$host.Version} -Credential $cred

Major  Minor  Build  Revision PSComputerName
-----  -----  -----  -------- --------------
1      0      0      0        10.0.0.5

Danger, Will Robinson!

Registry

If you don’t want to open up PowerShell itself, you can also check the registry. The version of PowerShell is tucked away under a value in the registry key path HKLM:\SOFTWARE\Microsoft\PowerShell\3\PowerShellEngine. This registry key has a value called PowerShellVersion that you can reference by using Get-ItemProperty.

PS51> (Get-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\PowerShell\3\PowerShellEngine -Name 'PowerShellVersion').PowerShellVersion
5.1.17134.1

You can see that this version is similar but doesn’t include the revision like the other options do.

PS51> [version](Get-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\PowerShell\3\PowerShellEngine -Name 'PowerShellVersion').PowerShellVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      17134  1

Using Other Tools

Using the registry also means you don’t need to use PowerShell at all to find the version. You can run commands from the command prompt or another tool that can read the registry.

CMD> reg query HKLM\SOFTWARE\Microsoft\PowerShell\3\PowerShellEngine /v PowerShellVersion

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\3\PowerShellEngine
    PowerShellVersion    REG_SZ    5.1.17134.1

Registry on Remote Computers

The registry is static and the values won’t change locally or remotely. You can be confident that what you see locally will be the same as you see remotely.

PS51> $scriptBlock = {
    [version](Get-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\PowerShell\3\PowerShellEngine -Name 'PowerShellVersion').PowerShellVersion
}
PS51> Invoke-Command -ComputerName 10.0.0.5 -ScriptBlock $scriptBlock -Credential $cred

Major  Minor  Build  Revision PSComputerName
-----  -----  -----  -------- --------------
5      1      17763  1        10.0.0.5

Showing the same version locally and remotely is good. But I’ve got a better way to show you using the $PSVersionTable automatic variable.

Check Powershell Version via $PSVersionTable.PSVersion command

The last and final method is referencing the PSVersion property on the $PSVersionTable automatic variable. This method will always represent the PowerShell engine.

PS51> $PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      17134  858

The $PSVersionTable automatic variable is a read-only hash table that returns information specifically about the PowerShell engine version. This automatic variable not only returns the version but also PSEdition. This property can either be Core or Desktop to provide further information on the edition of PowerShell that is running.

$PSVersionTable output
$PSVersionTable output

$PSVersionTable on Remote Computers

Using the $PSVersionTable automatic variable is accurate locally as it is remotely. You can see below that by wrapping $PSVersionTable.PSVersion in a scriptblock and executing that code on a remote computer, it will return the same version.

PS> Invoke-Command -ComputerName 10.0.0.5 -ScriptBlock {$PSVersionTable.PSVersion} -Credential $cred

Major  Minor  Build  Revision PSComputerName
-----  -----  -----  -------- --------------
5      1      17763  592      10.0.0.5

Summary

In this blog post, you have learned all of the ways to check the version of PowerShell both locally and remotely. I hope the first few methods gave you an idea of which ways not to check the version!

I recommend always using $PSVersionTable.PSVersion. All of the other methods may appear similar to the PowerShell engine version but may not always reflect the engine version.

If I missed any way, please let me know in the comments. I’ll be glad to update the post and give you credit.

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!