How can I determine which version of PowerShell is installed on a computer, and verify if PowerShell is installed at all?
You can determine which version of PowerShell is installed by using the $PSVersionTable command in PowerShell, which displays detailed version information, or by running $PSVersionTable.PSVersion to get just the version number. To verify if PowerShell is installed at all, you can check for the existence of the PowerShell executable using commands like powershell --version or by looking for the installation path in system directories.
Contents
- Basic PowerShell Version Detection
- Checking PowerShell Installation Status
- Platform-Specific Detection Methods
- Advanced Version Information
- Automated Version Checking Scripts
- Troubleshooting Common Issues
Basic PowerShell Version Detection
The simplest way to check your PowerShell version is to run the following command in a PowerShell terminal:
$PSVersionTable
This command displays a comprehensive table containing detailed information about your PowerShell installation, including the version number, build number, and other relevant details. The output typically looks like this:
Name Value
---- -----
PSVersion 7.4.0
PSEdition Core
GitCommitId 7.4.0
OS Microsoft Windows 10.0.19045
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
To get just the version number, you can use:
$PSVersionTable.PSVersion
For a quick version display:
$PSVersionTable.PSVersion.ToString()
Checking PowerShell Installation Status
Method 1: Using PowerShell Commands
If you suspect PowerShell might not be installed, you can try to run PowerShell and check for errors:
# Try to access PowerShell variables
$PSVersionTable 2>$null
if ($?) {
Write-Host "PowerShell is installed"
} else {
Write-Host "PowerShell is not available"
}
Method 2: Command Line Verification
From any command prompt (cmd.exe), you can check for PowerShell installation:
powershell --version
If PowerShell is installed, this will display the version number. If not, you’ll get an error message.
Method 3: Checking Installation Directory
You can verify PowerShell installation by checking common installation paths:
# Check typical installation paths
$paths = @(
"$env:ProgramFiles\PowerShell\*\pwsh.exe",
"$env:ProgramFiles\PowerShell\*\pwsh.dll",
"$env:ProgramFiles(x86)\PowerShell\*\pwsh.exe",
"$env:ProgramFiles(x86)\PowerShell\*\pwsh.dll"
)
foreach ($path in $paths) {
Get-Item $path -ErrorAction SilentlyContinue | Select-Object -First 1
}
Platform-Specific Detection Methods
Windows Systems
On Windows, both Windows PowerShell (version 5.1 and earlier) and PowerShell Core (version 6+) can be installed. Here’s how to distinguish between them:
For Windows PowerShell:
# Check if running in Windows PowerShell
if ($PSVersionTable.PSEdition -eq 'Desktop') {
Write-Host "Running Windows PowerShell $($PSVersionTable.PSVersion)"
} else {
Write-Host "Running PowerShell Core $($PSVersionTable.PSVersion)"
}
PowerShell 7+ Detection:
# Check if PowerShell 7+ is installed
$pwshPath = "$env:ProgramFiles\PowerShell\*\pwsh.exe"
Get-Item $pwshPath -ErrorAction SilentlyContinue | Select-Object -First 1
macOS and Linux Systems
On macOS and Linux, only PowerShell Core is available:
# Check PowerShell version from bash
pwsh --version
# Or check if PowerShell is installed
which pwsh
In PowerShell itself on these platforms:
# Platform-specific information
$PSVersionTable.Platform
$PSVersionTable.OS
# Check if running on macOS or Linux
if ($PSVersionTable.Platform -eq 'Unix') {
$unixType = if ($PSVersionTable.OS -like '*Darwin*') { 'macOS' } else { 'Linux' }
Write-Host "Running on $unixType"
}
Cross-Platform Detection Script
Here’s a comprehensive script that works across all platforms:
function Get-PowerShellVersion {
param(
[switch]$Detailed
)
if ($PSVersionTable) {
$version = $PSVersionTable.PSVersion
$edition = $PSVersionTable.PSEdition
$platform = $PSVersionTable.Platform
if ($Detailed) {
[PSCustomObject]@{
Version = $version.ToString()
Major = $version.Major
Minor = $version.Minor
Patch = $version.Patch
Build = $version.Build
Revision = $version.Revision
Edition = $edition
Platform = $platform
GitCommitId = $PSVersionTable.GitCommitId
OS = $PSVersionTable.OS
}
} else {
"$edition $version"
}
} else {
"PowerShell not detected"
}
}
# Usage
Get-PowerShellVersion
Get-PowerShellVersion -Detailed
Advanced Version Information
Complete Version Properties
PowerShell versions contain multiple properties that provide detailed information:
$version = $PSVersionTable.PSVersion
Write-Host "Major version: $($version.Major)"
Write-Host "Minor version: $($version.Minor)"
Write-Host "Patch version: $($version.Patch)"
Write-Host "Build number: $($version.Build)"
Write-Host "Revision: $($version.Revision)"
Write-Host "Full version string: $($version.ToString())"
Comparing PowerShell Versions
You can compare PowerShell versions programmatically:
function Compare-PowerShellVersion {
param(
[Version]$TargetVersion,
[switch]$OrHigher
)
$currentVersion = $PSVersionTable.PSVersion
if ($OrHigher) {
return $currentVersion -ge $TargetVersion
} else {
return $currentVersion -eq $TargetVersion
}
}
# Examples
Compare-PowerShellVersion -TargetVersion "7.0.0" -OrHigher
Compare-PowerShellVersion -TargetVersion "5.1" -OrHigher
Detecting Specific PowerShell Features
Different versions introduce different features. Here’s how to check for specific version capabilities:
# Check for PowerShell 7+ features
if ($PSVersionTable.PSVersion -ge "7.0.0") {
Write-Host "PowerShell 7+ features available"
# Features like parallelForEach, ternary operator, etc.
}
# Check for Windows PowerShell specific features
if ($PSVersionTable.PSEdition -eq 'Desktop') {
Write-Host "Windows Desktop edition features available"
# Features like Windows-specific cmdlets
}
# Check for experimental features
if ($PSVersionTable.ContainsKey('PSVersion') -and
$PSVersionTable.PSVersion -ge "7.2.0") {
Write-Host "Experimental features available"
}
Automated Version Checking Scripts
Comprehensive Detection Script
Here’s a complete script that checks for PowerShell installation and provides detailed version information:
<#
.SYNOPSIS
Comprehensive PowerShell version detection script
.DESCRIPTION
Detects PowerShell installation and provides detailed version information
across all supported platforms
#>
function Get-CompletePowerShellInfo {
[CmdletBinding()]
param(
[switch]$IncludePaths,
[switch]$IncludeAllDetails
)
$result = [ordered]@{
PowerShellDetected = $false
Version = $null
Edition = $null
Platform = $null
OS = $null
InstallationPaths = @()
AdditionalInfo = @{}
}
# Check if PowerShell is running
if ($PSVersionTable) {
$result.PowerShellDetected = $true
$result.Version = $PSVersionTable.PSVersion.ToString()
$result.Edition = $PSVersionTable.PSEdition
$result.Platform = $PSVersionTable.Platform
$result.OS = $PSVersionTable.OS
# Get installation paths
if ($IncludePaths) {
switch ($result.Platform) {
'Win32NT' {
$paths = @(
"$env:ProgramFiles\PowerShell\*\pwsh.exe",
"$env:ProgramFiles(x86)\PowerShell\*\pwsh.exe",
"$env:SystemRoot\System32\WindowsPowerShell\v1.0\powershell.exe"
)
}
'Unix' {
$paths = @(
"/usr/local/bin/pwsh",
"/usr/bin/pwsh",
"/opt/microsoft/powershell/*/pwsh"
)
}
}
foreach ($path in $paths) {
$found = Get-Item $path -ErrorAction SilentlyContinue |
Select-Object -First 1
if ($found) {
$result.InstallationPaths += $found.FullName
}
}
}
# Additional information
if ($IncludeAllDetails) {
$result.AdditionalInfo = @{
GitCommitId = $PSVersionTable.GitCommitId
PSRemotingProtocolVersion = $PSVersionTable.PSRemotingProtocolVersion
SerializationVersion = $PSVersionTable.SerializationVersion
WSManStackVersion = $PSVersionTable.WSManStackVersion
PSCompatibleVersions = $PSVersionTable.PSCompatibleVersions -join ', '
}
}
}
[PSCustomObject]$result
}
# Usage examples
Get-CompletePowerShellInfo
Get-CompletePowerShellInfo -IncludePaths -IncludeAllDetails
Cross-Platform Batch Script
For environments where you need to check PowerShell from cmd.exe or bash:
@echo off
REM PowerShell version detection for Windows cmd.exe
powershell -Command "if ($PSVersionTable) { $PSVersionTable.PSVersion } else { 'PowerShell not installed' }" 2>nul
if %ERRORLEVEL% EQU 0 (
echo PowerShell is installed
) else (
echo PowerShell is not installed or not in PATH
)
#!/bin/bash
# PowerShell version detection for Linux/macOS bash
if command -v pwsh &> /dev/null; then
echo "PowerShell is installed"
pwsh --version
else
echo "PowerShell is not installed"
exit 1
fi
Troubleshooting Common Issues
PowerShell Not Found in PATH
If PowerShell commands aren’t recognized, the installation might not have added PowerShell to the system PATH:
Windows:
# Check current PATH
$env:PATH -split ';'
# Add PowerShell to PATH (requires admin privileges)
$pwshPath = (Get-Command pwsh).Source
$env:PATH += ";$pwshPath"
Linux/macOS:
# Add PowerShell to PATH
export PATH="$PATH:/usr/local/bin"
# Or for system-wide installation
sudo ln -s /opt/microsoft/powershell/*/pwsh /usr/local/bin/pwsh
Multiple PowerShell Versions Installed
When you have multiple PowerShell versions installed, you might need to specify which one to use:
# List all available PowerShell versions
Get-ChildItem "$env:ProgramFiles\PowerShell" |
Where-Object { $_.Name -match '^[0-9]' } |
Select-Object Name, FullName
# Run specific PowerShell version
& "$env:ProgramFiles\PowerShell\7\pwsh.exe" -Command "Write-Host 'PowerShell 7'"
Permission Issues
On some systems, you might encounter permission issues when trying to run PowerShell:
# Check execution policy
Get-ExecutionPolicy
# Set execution policy (requires admin privileges)
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
# Run PowerShell with specific policy
powershell -ExecutionPolicy Bypass -File script.ps1
Version Compatibility Issues
When working with scripts across different PowerShell versions:
# Check minimum required version
$minimumVersion = "5.1"
if ($PSVersionTable.PSVersion -lt $minimumVersion) {
Write-Error "PowerShell version $($PSVersionTable.PSVersion) is not supported. Minimum required: $minimumVersion"
exit 1
}
# Use version-specific commands
if ($PSVersionTable.PSVersion -ge "7.0.0") {
# PowerShell 7+ specific code
$result = 1..1000 | ForEach-Object -Parallel { $_ * 2 } -ThrottleLimit 10
} else {
# Legacy PowerShell code
$result = 1..1000 | ForEach-Object { $_ * 2 }
}
Conclusion
Determining your PowerShell version and installation status is straightforward using the methods described above. For quick verification, use $PSVersionTable in PowerShell or powershell --version from the command line. The key takeaways are:
- Use
$PSVersionTablefor comprehensive version information in PowerShell - Check
$PSVersionTable.PSEditionto distinguish between Windows PowerShell and PowerShell Core - Use platform-specific methods for macOS and Linux systems
- PowerShell 7+ offers cross-platform compatibility while maintaining backward compatibility
- Automated scripts can help manage version detection across multiple systems
For system administrators and developers, understanding which PowerShell version is installed is crucial for script compatibility and troubleshooting. The provided scripts and methods work across different platforms and can be integrated into larger automation workflows.