There are numerous ways to do the same thing in Powershell. In this post, we will go over every way to check the PowerShell version. All these ways should work in both Windows PowerShell and PowerShell Core.
Here are the ways to get the version of PowerShell :
- The $host.Version property
- The (Get-Host).Version property
- The $PSVersionTable.PSVersion property
- The registry (Windows PowerShell only)
Another method is getting the Powershell version from a remote computer:
Invoke-Command -ComputerName 10.0.0.5 -ScriptBlock {Get-Host} -Credential $cred
results in for example:
Using $host and Get-Host command
$host.Version on Remote Computers
You will see the same behavior via PowerShell Remoting with $host.Version
as you will running Get-Host
.
Invoke-Command -ComputerName 10.0.0.5 -ScriptBlock {$host.Version} -Credential $cred
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
.
(Get-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\PowerShell\3\PowerShellEngine -Name 'PowerShellVersion').PowerShellVersion 5.1.17134.1
Also WMI:
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 a remote computer:
$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
Version via $PSVersionTable.
PSVersion
property on the $PSVersionTable
automatic variable. This method will always represent the PowerShell engine.
No comments:
Post a Comment