Everything about determining your PowerShell version

From Svendsen Tech PowerShell Wiki
Jump to: navigation, search

There are a few ways to find which version of PowerShell you're running. $PSVersionTable.PSVersion exists in v2 and up, and you can use $Host.Version in v1.x - so long as you make sure you use powershell.exe or powershell_ise.exe. Other hosting applications, such as PowerGUI, set their own version numbers in $Host.Version and (Get-Host).Version (correctly so).

If you've looked at the directory structure and found "v1.0" - don't be fooled - it can still be Powershell v2 or later. From what I've read, Microsoft originally had planned to use different directories, but later changed their mind - for backwards compatibility reasons if I am to venture a guess. So now they, and we, are stuck with this "v1.0" folder.

There's a list of all the different PowerShell executable file system locations here in this article.

Also see this article about a script for finding which version(s) of .NET are installed on your local or even remote workstations or servers. I heavily recommend the "DotNetVersionLister" module there. I've noticed most people seem to prefer (dot-sourcable) .ps1 files even if inferior in quality and not being recommended by the author, when downloading from the internet. You can install DotNetVersionLister from the PowerShell gallery, a Microsoft project and online repository for scripts.




Screenshot

How-to-find-PowerShell-version-including-major-and-minor.png

Displaying PowerShell version with PowerShell v2 and up

  • With PowerShell v2 and up, you can enter $PSVersionTable at the prompt, and look at the "PSVersion" property:
PS C:\> $PSVersionTable

Name                           Value
----                           -----
CLRVersion                     2.0.50727.5446
BuildVersion                   6.1.7601.17514
PSVersion                      2.0
WSManStackVersion              2.0
PSCompatibleVersions           {1.0, 2.0}
SerializationVersion           1.1.0.1
PSRemotingProtocolVersion      2.1

To index into a property of the object and display only the PSVersion property, and then further into the "Major" property under there, you can just use the dot operator.

PS C:\temp> $PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
4      0      -1     -1      



PS C:\temp> $PSVersionTable.PSVersion.Major
4

PS C:\temp> 

You can also call the .ToString() method on the PSVersion property of the $PSVersionTable variable for a nice display:

PS C:\> $PSVersionTable.PSVersion.ToString()
5.1.15063.608

Displaying major and minor version separated by a period

PS C:\> [String] $PSVersionTable.PSVersion.Major + "." + $PSVersionTable.PSVersion.Minor
5.1

PS C:\> "$($PSVersionTable.PSVersion.Major).$($PSVersionTable.PSVersion.Minor)"
5.1

Displaying PowerShell version with PowerShell v1

With PowerShell version 1, you can inspect the variable $Host and the property Version to see which version of PowerShell is running. You need to make sure you use powershell.exe or powershell_ise.exe, though, not PowerGUI or another hosting application, because these (may) set their own version here.

PowerShell version 1 doesn't have the $PSVersionTable variable mentioned above. The $Host variable is in PowerShell v2 as well.

Currently this is the output from PowerShell v2 (I actually don't have any computer still running v1 around):

PS C:\> $host.Version

Major  Minor  Build  Revision
-----  -----  -----  --------
2      0      -1     -1

You can also create a simple function that checks for the existence of $PSVersionTable, introduced in PowerShell version 2, and if it doesn't exist, it returns "1.0".

Possibly throw on -ErrorAction SilentlyContinue for Test-Path.

PS C:\temp> function Get-PSVersion {
    if (Test-Path Variable:\PSVersionTable) { $PSVersionTable.PSVersion } else { [version]'1.0' }
}

PS C:\temp> Get-PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
4      0      -1     -1      

Using Get-Host

You can also use the cmdlet Get-Host, and look at the "Version" property:

PS C:\> Get-Host


Name             : ConsoleHost
Version          : 2.0
InstanceId       : a3a9b37c-61a4-478d-bcf6-b3e0f1b93f26
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : nb-NO
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace

PS C:\temp> (Get-Host).Version

Major  Minor  Build  Revision
-----  -----  -----  --------
4      0      -1     -1      



PS C:\temp> (Get-Host).Version.Major
4

PS C:\temp> 

More obscure

Yet another way, that returns the same as "$Host.Version" in my PowerShell v2, is looking at the default runspace:

PS C:\> [System.Management.Automation.Runspaces.Runspace]::DefaultRunspace.Version

Major  Minor  Build  Revision
-----  -----  -----  --------
2      0      -1     -1