Development

How to Show the PowerShell Version

Captain Salem 2 min read

How to Show the PowerShell Version

Welcome to another PowerShell tutorial. In this one, we will discuss how we can get the version of our current PowerShell instance.

Although it can sound trivial, knowing the PowerShell version can help you determine the features to include in your scripts. You can also use the enumerated PowerShell versions to scope your scripts to specific versions. When scripting, it is always important to try and cover all environments

Let’s dive in.

Method 1 - Using $PSVersionTable cmdlet

PowerShell provides us with the $PSVersionTable command which allows you to get the version of your PowerShell instance. The commands returns the resulting information in tabular format.

Example:

$PSVersionTable

The command should return information such as the PowerShell version, PowerShell Edition, Operating system, etc. An example output:

Name                           Value
----                           -----
PSVersion                      7.2.6
PSEdition                      Core
GitCommitId                    7.2.6
OS                             Microsoft Windows 10.0.25206
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

If you are only interested in the PowerShell version, you can access the PSVersion attribute using the dot notation.

$PSVersionTable.PSVersion

The command above should show the detailed information about the PowerShell version, such as major version, minor, etc.

Major  Minor  Patch  PreReleaseLabel BuildLabel
-----  -----  -----  --------------- ----------
7      2      6

Method 2 - Using Get-Host Cmdlet

Another common method of fetching the PowerShell version is using the Get-Host cmdlet. This command returns an object holding info about the host system.

Get-Host

Resulting output:

Name             : ConsoleHost
Version          : 7.2.6
InstanceId       : 2907d9e1-153e-4a8d-90ec-2f14a911cdfc
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-US
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled  : True
IsRunspacePushed : False

To get the details of the PowerShell build, access the Version attribute as shown:

(Get-Host).Version

Output:

Major  Minor  Build  Revision
-----  -----  -----  --------
7      2      6      -1

Method 3 - Using $Host Command

If for some reason you do not have access to the above commands, you can use the $Host command to show the PowerShell version from the host system.

Example:

$Host

Results:

Name             : ConsoleHost
Version          : 7.2.6
InstanceId       : 2907d9e1-153e-4a8d-90ec-2f14a911cdfc
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

To fetch the powershell version, run the command:

$Host.version

The resulting output:

Major  Minor  Build  Revision
-----  -----  -----  --------
7      2      6      -1

Keep in mind that the above command shows the version of the host and not the actual PowerShell instance.

Final

In this post, we learned how to determine the PowerShell version using various commands and cmdlets.

We hope you enjoyed this tutorial, leave us a comment down below and share with your friends.

Thanks fo reading, catch you in the next one.

Share
Comments
More from Cloudenv

Cloudenv

Developer Tips, Tricks and Tutorials.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Cloudenv.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.