Determine installed PowerShell version
Use $PSVersionTable.PSVersion
to determine the engine version. If the variable does not exist, it is safe to assume the engine is version 1.0
.
Note that $Host.Version
and (Get-Host).Version
are not reliable - they reflect
the version of the host only, not the engine. PowerGUI,
PowerShellPLUS, etc. are all hosting applications, and
they will set the host's version to reflect their product
version — which is entirely correct, but not what you're looking for.
PS C:\> $PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
4 0 -1 -1
I would use either Get-Host or $PSVersionTable. As Andy Schneider points out, $PSVersionTable
doesn't work in version 1; it was introduced in version 2.
get-host
Name : ConsoleHost
Version : 2.0
InstanceId : d730016e-2875-4b57-9cd6-d32c8b71e18a
UI : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture : en-GB
CurrentUICulture : en-US
PrivateData : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
IsRunspacePushed : False
Runspace : System.Management.Automation.Runspaces.LocalRunspace
$PSVersionTable
Name Value
---- -----
CLRVersion 2.0.50727.4200
BuildVersion 6.0.6002.18111
PSVersion 2.0
WSManStackVersion 2.0
PSCompatibleVersions {1.0, 2.0}
SerializationVersion 1.1.0.1
PSRemotingProtocolVersion 2.1
You can look at the built in variable, $psversiontable
. If it doesn't exist, you have V1. If it does exist, it will give you all the info you need.
1 > $psversiontable
Name Value
---- -----
CLRVersion 2.0.50727.4927
BuildVersion 6.1.7600.16385
PSVersion 2.0
WSManStackVersion 2.0
PSCompatibleVersions {1.0, 2.0}
SerializationVersion 1.1.0.1
PSRemotingProtocolVersion 2.1
To determine if PowerShell is installed, you can check the registry for the existence of
HKEY_LOCAL_MACHINE\Software\Microsoft\PowerShell\1\Install
and
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\3
and, if it exists, whether the value is 1 (for installed), as detailed in the blog post Check if PowerShell installed and version.
To determine the version of PowerShell that is installed, you can check the registry keys
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine\PowerShellVersion
and
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\3\PowerShellEngine\PowerShellVersion
To determine the version of PowerShell that is installed from a .ps1 script, you can use the following one-liner, as detailed on PowerShell.com in Which PowerShell Version Am I Running.
$isV2 = test-path variable:\psversiontable
The same site also gives a function to return the version:
function Get-PSVersion {
if (test-path variable:psversiontable) {$psversiontable.psversion} else {[version]"1.0.0.0"}
}