PowerShell is a shell scripting environment developed by Microsoft with more than 10 years of history. It comes with two editions: one named as Windows PowerShell which is built on top of .NET Framework, and the other named PowerShell Core, built on .NET Core. As the .NET Framework is Windows-only, the Windows PowerShell is only available on Windows as well, while PowerShell Core is cross-platform thanks to the cross-platform nature of .NET Core.

Runtime Check

With the rapid development of PowerShell, the need to do version detection in PowerShell scripts arises occasionally.

Checking PowerShell Version

Note: $PSVersionTable variable is introduced since PowerShell 2. So PowerShell of version 1 can be assumed if nothing returned from it.

e.g. Run on Windows 10 Version 1607 (OS Build 14393.1593):

PS> $PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      14393  1532


e.g. Run with installation of PowerShell Core:

PS> $PSVersionTable.PSVersion

Major Minor Patch Label
----- ----- ----- -----
    6     0     0 beta


The Get-Host cmdlet and $host variable return the same object, so using any of them is equivalent. However, they actually reflect the version of the host environment, not necessarily the PowerShell version, which only works in local console host.

For example, run the command on local machine:

PS> Get-Host


Name             : ConsoleHost
Version          : 5.1.14393.1532
InstanceId       : c6415761-c4b2-4be9-8f10-925d9dc8d661
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-US
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled  : True
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace



And run it in a remote session (via Enter-PSSession cmdlet):

PS> $host


Name             : ServerRemoteHost
Version          : 1.0.0.0
InstanceId       : 7d80f8ed-9fdf-4ab6-90fb-50eaf663ccab
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-US
CurrentUICulture : en-US
PrivateData      :
IsRunspacePushed :
Runspace         : System.Management.Automation.Runspaces.LocalRunspace



To extract the version, use $host.Version or $(Get-Host).Version or Get-Host | Select-Object Version.

References

Checking PowerShell Edition

As we've mentioned before, PowerShell comes with two editions: Windows PowerShell and PowerShell Core. Use $PSVersionTable.PSEdition to check it.

PS> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      6.0.0-beta
PSEdition                      Core
GitCommitId                    v6.0.0-beta.6
OS                             Microsoft Windows 10.0.14393
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0


Desktop is Windows PowerShell, while Core is PowerShell Core.

If this variable does not exist, it implies Desktop.

Checking PowerShell Architecture

PowerShell can run in both 32-bit and 64-bit environment, to test the current running environment architecture, we may use one of several methods.

AMD64 for 64-bit and x86 for 32-bit.

PS> $Env:PROCESSOR_ARCHITECTURE
AMD64

2. Check [Environment]::Is64BitProcess

This property is available since PowerShell 3.0:

PS> [Environment]::Is64BitProcess
True

3. Check [IntPtr]::Size

IntPtr.Size Property (System):

The value of this property is 4 in a 32-bit process, and 8 in a 64-bit process.

Reference

Installation Check

Besides runtime version check, we may also need to check the installation of PowerShell.

Checking via Registry

  • To check if any version of PowerShell is installed, check for the following value in the registry:

    Key Location: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1
    Value Name: Install
    Value Type: REG_DWORD
    Value Data: 0x00000001 (1)

  • To check whether version 1.0 or 2.0 of PowerShell is installed, check for the following value in the registry:

    Key Location: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine
    Value Name: PowerShellVersion
    Value Type: REG_SZ
    Value Data: <1.0 | 2.0>

  • To check whether version 3.0, 4.0, 5.0 or 5.1 of PowerShell is installed, check for the following value in the registry:

    Key Location: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\3\PowerShellEngine
    Value Name: PowerShellVersion
    Value Type: REG_SZ
    Value Data: <3.0 | 4.0.x.y | 5.0.x.y | 5.1.x.y>

Reference: Detection logic for PowerShell installation | PowerShell Team Blog

Checking via File System

Regardless of PowerShell version, Windows PowerShell is always installed to %SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe,
while PowerShell Core is installed to %ProgramFiles%\PowerShell\<version>\powershell.exe by default.

Reference