PowerShell: The $PSVersionTable Variable

Powershell @ Freshers.in

PowerShell is a powerful scripting language and command-line shell designed by Microsoft. It is built on the .NET framework and enables administrators to perform administrative tasks on both local and remote systems. One of the essential features within PowerShell that both beginners and experienced users come across is the $PSVersionTable variable. This article aims to demystify the $PSVersionTable variable, explaining its purpose, significance, and how to use it effectively with practical examples.

Understanding the $PSVersionTable Variable

The $PSVersionTable variable is an intrinsic part of PowerShell that provides a wealth of information about the PowerShell environment you are currently using. It is a read-only variable that holds details such as the version of PowerShell, CLR (Common Language Runtime) version, edition, and other related platform information.

Why Is $PSVersionTable Important?

Knowing the version of PowerShell you’re working with is crucial for several reasons:

  • Compatibility: Ensures scripts will run as expected across different environments by checking version compatibility.
  • Troubleshooting: Helps in diagnosing and resolving issues related to version-specific features or bugs.
  • Feature Availability: Different versions of PowerShell introduce new features and deprecate old ones. Knowing your version helps you understand the features available to you.

Exploring the $PSVersionTable Variable

To view the contents of the $PSVersionTable variable, simply enter it into your PowerShell prompt:

This command outputs information similar to the following (note that the exact output depends on your specific version of PowerShell):

Name                           Value
----                           -----
PSVersion                      7.1.3
PSEdition                      Core
GitCommitId                    7.1.3
OS                             Microsoft Windows 10.0.19042
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
WSManStackVersion              3.0

Key Components of $PSVersionTable

  • PSVersion: The version of PowerShell.
  • PSEdition: Indicates whether you’re running PowerShell Core (cross-platform) or Windows PowerShell (Windows-specific).
  • GitCommitId: Often the same as PSVersion, but can include additional detail in development builds.
  • OS: The operating system PowerShell is running on.
  • Platform: The platform PowerShell is built on, such as Win32NT.
  • PSCompatibleVersions: Versions of PowerShell that are compatible with the current version.
  • PSRemotingProtocolVersion, SerializationVersion, WSManStackVersion: Technical details related to remoting and serialization.

Practical Examples

Checking if You Are Running a Specific Version of PowerShell

Suppose you have a script that requires at least PowerShell 5.1 to run. You can check the version like this:

if ($PSVersionTable.PSVersion -ge [Version]"5.1") {
    "This script can run."
} else {
    "This script requires PowerShell 5.1 or higher."

Determining if Running on PowerShell Core or Windows PowerShell

This is particularly useful for scripts that need to run across different platforms:

if ($PSVersionTable.PSEdition -eq "Core") {
    "Running on PowerShell Core."
} else {
    "Running on Windows PowerShell."
The $PSVersionTable variable is an essential tool in the PowerShell scripting language, offering critical information about the PowerShell environment. Understanding how to query and interpret this variable is fundamental for script compatibility, troubleshooting, and leveraging the capabilities of different PowerShell versions.
Author: user