The $PSVersionTable
automatic variable in PowerShell provides crucial version information about your PowerShell environment. Here's what each property represents:
# Example output structure
$PSVersionTable | Format-List -Property *
PSVersion
The core PowerShell engine version (e.g., 7.2.5). This is the most important version number for compatibility checks.
if ($PSVersionTable.PSVersion -ge [version]"5.1") {
Write-Host "You're running a modern PowerShell version"
}
CLRVersion
The version of .NET Common Language Runtime powering your PowerShell session. Critical for understanding framework compatibility.
BuildVersion
The specific build number of your PowerShell installation. Useful for troubleshooting exact patches.
WSManStackVersion
Version of WS-Management stack used for remoting capabilities. Affects WinRM functionality.
PSCompatibleVersions
List of PowerShell versions this installation claims compatibility with (e.g., {1.0, 2.0} for backward compatibility).
$PSVersionTable.PSCompatibleVersions | ForEach-Object {
Write-Host "Compatible with PowerShell $_"
}
SerializationVersion
Version of PowerShell's serialization format, important for remoting and workflow execution.
PSRemotingProtocolVersion
Version of PowerShell remoting protocol. Essential when troubleshooting cross-version remoting.
The PSVersion
property directly correlates with WMF versions:
- PSVersion 5.1.x → WMF 5.1
- PSVersion 5.0.x → WMF 5.0
- PSVersion 4.0 → WMF 4.0
- PSVersion 3.0 → WMF 3.0
# Check WMF version via PSVersion
$WMFVersion = switch -Wildcard ($PSVersionTable.PSVersion.ToString()) {
"5.1*" { "WMF 5.1" }
"5.0*" { "WMF 5.0" }
"4.0*" { "WMF 4.0" }
"3.0*" { "WMF 3.0" }
default { "Unknown WMF version" }
}
Write-Host "Detected: $WMFVersion"
Here's how to use this information in real scripts:
# Version requirement check
$requiredVersion = [version]"5.1"
if ($PSVersionTable.PSVersion -lt $requiredVersion) {
throw "This script requires PowerShell $requiredVersion or higher"
}
# Cross-version compatibility
if ($PSVersionTable.PSCompatibleVersions -contains [version]"2.0") {
Write-Host "Legacy PowerShell 2.0 compatibility mode available"
}
# .NET framework check
if ($PSVersionTable.CLRVersion.Major -ge 4) {
Write-Host "Using modern .NET CLR"
}
- In PowerShell Core (6.0+), some properties like CLRVersion are replaced with Core-specific equivalents
- The table structure remains consistent across Windows PowerShell and PowerShell Core
- For production scripts, always validate against PSVersion first
The $PSVersionTable
automatic variable in PowerShell provides crucial version information about the PowerShell environment and its underlying components. Here's a detailed breakdown of each property:
# Example output from PowerShell 5.1
PS C:\> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.19041.1320
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.19041.1320
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
PSVersion
Represents the current PowerShell engine version. This is the most commonly referenced version number and corresponds to the Windows Management Framework (WMF) version when running on Windows.
# Checking WMF version through PSVersion
$WMFVersion = $PSVersionTable.PSVersion
Write-Output "Windows Management Framework version: $($WMFVersion.ToString())"
PSEdition
Indicates whether you're running PowerShell Core ("Core") or Windows PowerShell ("Desktop"). Introduced in PowerShell 5.1.
PSCompatibleVersions
An array of versions that the current PowerShell engine can execute scripts designed for. Important for backward compatibility checks.
# Checking script compatibility
$targetVersion = [version]"3.0"
if ($PSVersionTable.PSCompatibleVersions -contains $targetVersion) {
Write-Output "Script compatible with this PowerShell version"
}
BuildVersion
Specific build number of the PowerShell engine, particularly useful for debugging and patch-level identification.
CLRVersion
The version of the Common Language Runtime (CLR) that PowerShell is running on. Critical for .NET interoperability.
# Checking .NET Framework compatibility
if ($PSVersionTable.CLRVersion -ge [version]"4.0.30319") {
Write-Output "Supports .NET Framework 4.0+ features"
}
WSManStackVersion
Version of the WS-Management stack used for PowerShell remoting.
PSRemotingProtocolVersion
The version of the PowerShell remoting protocol being used.
SerializationVersion
Version of the serialization format used for PowerShell remoting and workflow persistence.
Version-Specific Feature Gating
if ($PSVersionTable.PSVersion -ge [version]"5.1") {
# Use classes feature introduced in 5.0
class MyClass {
[string]$Name
}
}
else {
Write-Warning "This script requires PowerShell 5.1 or later"
}
Module Compatibility Checking
$requiredPSVersion = [version]"7.0"
if ($PSVersionTable.PSVersion -lt $requiredPSVersion) {
throw "This module requires PowerShell $requiredPSVersion or higher"
}
The PSVersion
property directly correlates with the WMF version when running on Windows. For example:
- PSVersion 5.1 → WMF 5.1
- PSVersion 5.0 → WMF 5.0
- PSVersion 4.0 → WMF 4.0
This relationship is documented in Microsoft's WMF documentation and remains consistent across Windows installations.
On non-Windows systems or with PowerShell Core, the version mapping differs as WMF is Windows-specific. In these cases, PSVersion
reflects the PowerShell Core version (6.0, 7.0, etc.).