How to Check your PowerShell Version (All the Ways!)

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 check your PowerShell version on local and remote computers. We'll cover the bad ways and my recommended way.

If you'd like to go from PowerShell newbie to PowerShell guru, I recommend devouring this FREE mini-course on building a PowerShell tool. This is a step-by-step tutorial with full explanations and guidance from Adam the Automator!

There are websites that show various ways to check the version of PowerShell. But none that compiled a comprehensive list of all 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 least to 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.

host can have a version that is completely independent of PowerShell itselfThis 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

Copy

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 necessary always do that.

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

Copy

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.

$host.Version

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

 

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

Copy

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

Copy

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

Copy

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

Copy

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

$PSVersionTable.PSVersion

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

Copy

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

 

$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 C:\> 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

Copy

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 on 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.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值