Exploring the Command `$PSVersionTable` in PowerShell (with examples)
- Windows
- December 17, 2024
The $PSVersionTable
command is a read-only automatic variable in PowerShell designed to give detailed information about the version of PowerShell you are currently running, among other useful details. This information is invaluable for debugging scripts, ensuring compatibility, and understanding the capabilities of the PowerShell instance on your machine.
Understanding these details can help developers and system administrators manage their scripts more effectively, ensure compatibility with different systems, and decide when updates might be necessary.
Use case 1: Print a summary of the currently installed PowerShell version and edition
Code:
$PSVersionTable
Motivation:
Running $PSVersionTable
without any additional specs is the simplest way to output a complete summary of the current PowerShell environment. This includes the version number, edition, and additional info about the runtime, all of which are critical for understanding what features and modules are supported by your installation.
Explanation:
$PSVersionTable
: This automatic variable is invoked to print a table containing all key/version pairs related to the PowerShell environment. This includes information like PSVersion, PSEdition, and more.
Example Output:
Name Value
---- -----
PSVersion 7.2.5
PSEdition Core
GitCommitId 7.2.5
OS Microsoft Windows 10.0.19043
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Use case 2: Get the detailed (major, minor, build, and revision) version number of PowerShell
Code:
$PSVersionTable.PSVersion
Motivation:
By accessing the specific PSVersion
property, users can quickly ascertain the detailed versioning of the installed PowerShell environment. This is crucial for compatibility checks when deploying scripts and modules across different environments, ensuring that your code will execute correctly without any version-related issues.
Explanation:
$PSVersionTable.PSVersion
: This extracts just the version information, specifically returning an object that containsMajor
,Minor
,Build
, andRevision
numbers. These numbers give a precise description of the installed PowerShell version, beyond the simple major version.
Example Output:
Major Minor Build Revision
----- ----- ----- --------
7 2 5 0
Use case 3: List all supported PowerShell script versions that this PowerShell version supports
Code:
$PSVersionTable.PSCompatibleVersions
Motivation:
Compatibility is a major concern for script writers and developers who manage different environments and equipment. By using this command, one can view all the PowerShell versions the current environment is compatible with, ensuring that scripts relying on older versions will run without issues.
Explanation:
$PSVersionTable.PSCompatibleVersions
: This returns a list or array of all the supported PowerShell versions that the current installation is compatible with. It allows users to ensure backward compatibility for scripts built on older versions.
Example Output:
Major Minor Build Revision
----- ----- ----- --------
1 0 0 0
2 0 0 0
3 0 0 0
4 0 0 0
5 0 0 0
7 2 5 0
Use case 4: Get the latest Git commit ID where the currently-installed PowerShell version is based on
Code:
$PSVersionTable.GitCommitId
Motivation:
For developers and contributors who might be involved with PowerShell’s development or are utilizing latest features, the Git Commit ID is a useful reference for pinpointing the exact state of the source code from which the current version was built. This is particularly relevant for those who may be working on or managing community or personal forks.
Explanation:
$PSVersionTable.GitCommitId
: Available from PowerShell 6.0 and later, it provides the specific Git Commit ID of the source from which the current PowerShell was built. This is crucial for source control and understanding recent changes or fixes that may impact scripts and modules.
Example Output:
d9ba677b8f46dbc41aea0576e0c71ba6b2ab4f6c
Use case 5: Check whether the user is running PowerShell Core (6.0 or later) or the original “Windows PowerShell” (version 5.1 or below)
Code:
$PSVersionTable.PSEdition
Motivation:
With the introduction of PowerShell Core, it became vital to differentiate between the editions. Knowing whether you’re running PowerShell Core or Windows PowerShell affects compatibility, available features, and plugins, ensuring you leverage the correct set of features for your scripts.
Explanation:
$PSVersionTable.PSEdition
: This property tells if the instance is based on PowerShell Core or the classic “Windows PowerShell.” This distinction is essential, as it affects compatibility with modules, scripts, and certain environmental features.
Example Output:
Core
Conclusion:
The $PSVersionTable
command is a powerful tool in the PowerShell ecosystem, delivering vital version information and compatibility checks. By understanding and using its features, users can ensure their scripts run with the right settings, maintain compatibility across diverse environments, and access the exact details necessary to troubleshoot and optimize their PowerShell usage effectively.