Identifying Your PowerShell Version
PowerShell is a powerful task automation and configuration management framework from Microsoft. Knowing the version of PowerShell installed on a system is crucial for compatibility, utilizing specific features, and troubleshooting. This tutorial outlines several methods to determine the PowerShell version, ranging from simple in-shell commands to more robust programmatic approaches.
Understanding PowerShell Versions
PowerShell has evolved through several major versions, each introducing new features, improvements, and addressing security concerns. Key versions include:
- PowerShell 1.0: The initial release, offering basic scripting capabilities.
- PowerShell 2.0: Introduced significant enhancements, including remoting and background jobs.
- PowerShell 3.0 & 4.0: Focused on desired state configuration (DSC) and improved scripting tools.
- PowerShell 5.1: The last version of Windows PowerShell, widely used and supported.
- PowerShell 7.x: The cross-platform, open-source version built on .NET Core (now .NET). PowerShell 7 continues to receive feature updates and improvements.
- PowerShell 8.x: The latest major release, offering further advancements and performance optimizations.
Method 1: Using $PSVersionTable
The $PSVersionTable
automatic variable is the most direct way to retrieve PowerShell version information within a PowerShell session. It contains a table of key-value pairs related to the PowerShell environment.
$PSVersionTable.PSVersion
This command will output the major, minor, build, and revision numbers of the PowerShell engine. For example:
Major Minor Build Revision
----- ----- ----- --------
5 1 19041 -1
Important Note: The $PSVersionTable
variable was introduced in PowerShell 2.0. If you are running PowerShell 1.0, this variable will not exist. We’ll cover handling this case later.
Method 2: Using Get-Host
The Get-Host
cmdlet provides information about the PowerShell host application. While not directly the PowerShell version, it can be helpful in understanding the environment.
Get-Host
This will display details such as the host name, version, UI culture, and other properties. However, be aware that the host version doesn’t always reflect the underlying PowerShell engine version, especially when using third-party hosts like PowerGUI or PowerShellPlus.
Method 3: Checking for $PSVersionTable
Existence
To reliably determine if PowerShell 1.0 is being used (where $PSVersionTable
does not exist), you can use a conditional statement:
if (Test-Path variable:\PSVersionTable) {
$PSVersionTable.PSVersion
} else {
Write-Host "PowerShell 1.0"
}
This code checks if the $PSVersionTable
variable exists. If it does, it outputs the version information. Otherwise, it assumes PowerShell 1.0 is being used.
Method 4: Programmatic Version Retrieval with a Function
For more robust version retrieval in scripts, you can define a function:
function Get-PSVersion {
if (Test-Path variable:PSVersionTable) {
$PSVersionTable.PSVersion
} else {
[Version]"1.0.0.0" # Return a Version object for consistency
}
}
Get-PSVersion
This function encapsulates the version retrieval logic and provides a consistent output, even for PowerShell 1.0. It utilizes the [Version]
type to ensure a standardized format for the version number.
Method 5: External Command Execution
You can also determine the PowerShell version by executing a PowerShell command from another shell (like Command Prompt or Bash). This is useful when you need to get the version programmatically from an external process.
powershell -Command "$PSVersionTable.PSVersion"
or
powershell -Command "(Get-Variable PSVersionTable -ValueOnly).PSVersion"
This command launches PowerShell and executes the $PSVersionTable.PSVersion
command, printing the version to the console. The second version is more robust across shell environments.
Best Practices
- Prioritize
$PSVersionTable
: The$PSVersionTable
variable is the most reliable way to determine the PowerShell version within a PowerShell session. - Handle PowerShell 1.0: Always include a check for the existence of
$PSVersionTable
to gracefully handle PowerShell 1.0 environments. - Use Functions for Reusability: Encapsulate version retrieval logic in functions for cleaner and more reusable code.
- Consider External Execution: For external process integration, use the appropriate shell command to execute PowerShell and retrieve the version.