views:

9186

answers:

5

Hi,

How can I determine what version of PowerShell is installed on a computer, and indeed if it is installed at all?

Thanks, MagicAndi

+5  A: 

To determine if PowerShell is installed, you can check the registry, as detailed in the following post :

To determine the version of PowerShell that is installed, you can check the follow registry key:

  • HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine

To determine the version of PowerShell that is installed from a .ps1 script, you can use the following one-liner, as detailed on PowerShell.com

$isV2 = test-path variable:\psversiontable

The same site also gives a function to return the version:

function Get-PSVersion { 
    if (test-path variable:psversiontable) {$psversiontable.psversion} else {[version]"1.0.0.0"} 
}
MagicAndi
+2  A: 

To check if PowerShell is installed use:

HKLM\Software\Microsoft\PowerShell\1 Install ( = 1 )

To check if RC2 or RTM is installed use:

HKLM\Software\Microsoft\PowerShell\1 PID (=89393-100-0001260-00301) -- For RC2

HKLM\Software\Microsoft\PowerShell\1 PID (=89393-100-0001260-04309) -- For RTM

Source: this website

Kindness,

Dan

Daniel Elliott
+22  A: 

The variable $Host has a version property that can be used to access the version of PowerShell that is running. Host is a built in PowerShell variable so it's available to you once you start a PowerShell session. If you want to see all the variables avaliable to you type:

PS H:\> Set-Location variable:
PS H:\> Get-ChildItem

This will list them all showing the name of the variable and its value.

PowerShell 1.0

PS H:\> $Host.Version

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

PowerShell 2.0(Windows 7)

PS H:\> $Host.Version
Major  Minor  Build  Revision
-----  -----  -----  --------
2      0      -1      -1

Calling the version property will return a System.Version object.

As for determining if PowerShell is installed I'm not sure how you wish to do this obviously you can't do this from a PowerShell script, chicken and the egg :-) but if you want to do it manually just click start and run then type 'powershell' if PowerShell has been installed you'll be presented with the PowerShell console.

If you want to programmatically check if PowerShell has been installed check out this post, you could combine it with the other answers on accessing the registry to create a command line application in C# or VB.

Alan
I wouldn't rely on $Host.Version, the registry is better. I'm on XP and have only PS1 installed, as shown in the registry and file system, and when I do $Host.Version I get this: Major Minor Build Revision ----- ----- ----- -------- 2 0 -1 -1
Bratch
I'm voting this answer down with regret because I'm getting the same results as Bratch. I definitely only have PS1 installed (confirmed by trying to use some PS2 features), but $Host.version gives me 2,0,-1,-1.
Daniel Cassidy
Hmm... scratch that last comment. It seems that PS2 is installed, but that some features are unaccountably unavailable, and for some reason the installation directory is named '1.0'. Thanks Microsoft.
Daniel Cassidy
+2  A: 

You can look at the built in variable, $psversiontable. If it doesn't exist, you have V1. If it does exist, it will give you all the info you need.

1 >  $psversiontable

Name                           Value                                           
----                           -----                                           
CLRVersion                     2.0.50727.4927                                  
BuildVersion                   6.1.7600.16385                                  
PSVersion                      2.0                                             
WSManStackVersion              2.0                                             
PSCompatibleVersions           {1.0, 2.0}                                      
SerializationVersion           1.1.0.1                                         
PSRemotingProtocolVersion      2.1
Andy Schneider
+10  A: 

I would use either Get-Host or $PSVersionTable. As Andy Schneider points out, $PSVersionTable only works with version 2.

get-host

Name             : ConsoleHost
Version          : 2.0
InstanceId       : d730016e-2875-4b57-9cd6-d32c8b71e18a
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-GB
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace

$PSVersionTable

Name                           Value
----                           -----
CLRVersion                     2.0.50727.4200
BuildVersion                   6.0.6002.18111
PSVersion                      2.0
WSManStackVersion              2.0
PSCompatibleVersions           {1.0, 2.0}
SerializationVersion           1.1.0.1
PSRemotingProtocolVersion      2.1
Thomas Bratt