Powershell

How to Get PowerShell Version

Microsoft PowerShell is a shell that’s built using Microsoft .NET technology. It’s designed to aid developers in building efficient scripts and tools to automate and speed up various processes. It was originally inspired by other scripting languages like VBScript and batch files.

PowerShell combines the command-line shell that can run commands similar to a classic shell (bash, zsh, etc.) and a scripting language. PowerShell is object-oriented, similar to object-oriented programming.

This guide will demonstrate how to check the installed version of PowerShell using various PowerShell commands and variables.

Installing PowerShell

PowerShell was a Windows-exclusive feature and was available on all the major Windows releases when it first came out. Starting from PowerShell 6.0, it’s become free and open-source software, available for all the major platforms (Windows and Linux). It’s now available for all the major Linux distros.

For distro-specific installation instructions, check out the official Microsoft documentation on PowerShell installation on Linux. Thankfully, PowerShell is also available as a snap package on the Snapcraft store. The snap packages can be installed on a wide range of Linux distros without any modification.

Check out PowerShell on Snapcraft store.

The following command will install PowerShell snap.

$ sudo snap install powershell --classic

Let’s perform a quick verification if PowerShell was installed successfully.

$ pwsh

Getting PowerShell version

In any programming/scripting language, there are multiple ways to reach a certain destination. It applies to PowerShell as well. We can check the PowerShell version using a handful of methods. It revolves around multiple PowerShell built-in features.

Keeping track of the PowerShell version is important in various scenarios. The system admin may want to stay up-to-date to avoid running old PowerShell. For developers, script compatibility is necessary to maintain. For general/advanced users, having the latest PowerShell offers additional features and enhanced security (patches, bug fixes, and other improvements).

Check PowerShell version using $PSVersionTable

When running the command, it will print a PowerShell hash table containing various info about PowerShell.

Run the command in PowerShell:

$ $PSVersionTable

To get the PowerShell version only, use the following command.

$ $PSVersionTable.PSVersion

PowerShell allows fine-tuning the output to print only specific info. In the next demonstration, we’ll get the in-depth version of the running PowerShell.

$ $PSVersionTable.Values

If you’re running PowerShell 7 or later, it also comes with backward compatibility with older releases. Check the supported backward-compatible PowerShell releases.

$ $PSVersionTable.PSCompatibleVersions

Check PowerShell version using Get-Variable

As the name suggests, the command Get-Variable allows you to read the value of a variable in PowerShell. Using this command, we can extract the value of the variable $PSVersionTable. This method is the same as before, except we’ll be using Get-Variable to do the job. Have a quick look at checking environment variables in PowerShell.

To print the version of PowerShell, run the following command.

$ (Get-Variable PSVersionTable -ValueOnly).PSVersion

Check PowerShell version using Get-Host

In PowerShell, a host is a program that’s hosting the PowerShell engine. The Get-Host command offers a wealth of info about the host (including various PowerShell info).

$ Get-Host

To get the PowerShell version specifically, run the following command instead.

$ (Get-Host).Version

Alternatively, we can pass the output of Get-Host to Select-Object to show the “Version” field only.

$ Get-Host | Select-Object version

Check PowerShell version using $Host automatic variable

The $Host variable in PowerShell contains various information about the host. It also includes the PowerShell version.

$ $Host

To get specifically the PowerShell version, use the following command instead.

$ $Host.version

Final thoughts

This guide demonstrates all the possible ways of checking the version of PowerShell installed. It also showcases how to extract the info more conveniently with the help of some basic PowerShell scripting. For reliable version info, it’s always recommended to use $PSVersionTable.PSVersion as the reference.

PowerShell scripting can achieve a lot more. For beginners, check out this step-by-step guide on writing a PowerShell script.

Happy computing!

About the author

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.