How to use the command "PSVersionTable" (with examples)
- Windows
- November 5, 2023
The PSVersionTable
command is a read-only variable in PowerShell that allows users to retrieve information about the current PowerShell version. It provides various properties such as PSVersion
, PSCompatibleVersions
, GitCommitId
, and PSEdition
.
Use case 1: Print a summary of the currently installed PowerShell version and edition
Code:
$PSVersionTable
Motivation: This use case is helpful when you need to quickly check the installed PowerShell version and edition. It can be used for troubleshooting or verifying compatibility requirements.
Explanation: This command retrieves the entire PSVersionTable
object, which contains information about the PowerShell version, edition, compatible script versions, and more.
Example output:
Name Value
---- -----
PSVersion 7.2.0
PSEdition Core
GitCommitId 7.2.6
...
Use case 2: Get the detailed version number of PowerShell
Code:
$PSVersionTable.PSVersion
Motivation: Sometimes, you may need the detailed version number of the installed PowerShell, including the major, minor, build, and revision numbers. This information can be useful when troubleshooting or checking for compatibility with specific features.
Explanation: This command retrieves the PSVersion
property from the PSVersionTable
object, which contains the detailed version number of PowerShell.
Example output:
Major Minor Build Revision
----- ----- ----- --------
7 2 0 -1
Use case 3: Get a list of supported PowerShell script versions
Code:
$PSVersionTable.PSCompatibleVersions
Motivation: PowerShell versions may support different script versions. Having a list of supported script versions can help determine the compatibility of scripts and modules with the current PowerShell version.
Explanation: This command retrieves the PSCompatibleVersions
property from the PSVersionTable
object, which provides a list of PowerShell script versions supported by the current PowerShell version.
Example output:
Version PSEdition
------- ---------
1.0 Desktop
2.0 Desktop
3.0 Desktop
4.0 Desktop
5.0 Desktop
...
Use case 4: Get the latest Git commit ID
Code:
$PSVersionTable.GitCommitId
Motivation: PowerShell versions from 6.0 onwards are developed on GitHub. Retrieving the latest Git commit ID can help identify the specific GitHub commit associated with the installed PowerShell version.
Explanation: This command retrieves the GitCommitId
property from the PSVersionTable
object, which provides the latest Git commit ID associated with the currently installed PowerShell version.
Example output:
7.2.6
Use case 5: Check the PowerShell edition
Code:
$PSVersionTable.PSEdition
Motivation: PowerShell comes in two editions: “Windows PowerShell” (version 5.1 or below) and “PowerShell Core” (version 6.0 or later). Knowing the edition is crucial when dealing with platform-specific features or troubleshooting compatibility issues.
Explanation: This command retrieves the PSEdition
property from the PSVersionTable
object, which indicates whether the user is running “Windows PowerShell” or “PowerShell Core”.
Example output:
Core
Conclusion:
The PSVersionTable
command provides valuable information about the installed PowerShell version, edition, compatible script versions, Git commit ID, and more. It is an essential command for troubleshooting, checking compatibility, and verifying the PowerShell environment.