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.
Let’s perform a quick verification if PowerShell was installed successfully.
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:
To get the PowerShell version only, use the following command.
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.
If you’re running PowerShell 7 or later, it also comes with backward compatibility with older releases. Check the supported backward-compatible PowerShell releases.
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.
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).
To get the PowerShell version specifically, run the following command instead.
Alternatively, we can pass the output of Get-Host to Select-Object to show the “Version” field only.
Check PowerShell version using $Host automatic variable
The $Host variable in PowerShell contains various information about the host. It also includes the PowerShell version.
To get specifically the PowerShell version, use the following command instead.
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!