Wednesday, October 07, 2009

Ensuring PowerShell Is Loaded Onto a System

I saw a cool tip over at PowerShell.com for working out if PowerShell is available on a system. This tip points out that if PowerShell is installed on a machine, then the registry key HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\PowerShell\1 will exist and will contain key configuration information.

From my main workstation, I see:

PSH [C:\]: cd hklm:\\SOFTWARE\MICROSOFT\PowerShell\1\
HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\PowerShell\1
PSH [HKLM:\SOFTWARE\MICROSOFT\PowerShell\1]: ls

    Hive: HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\PowerShell\1

SKC  VC Name                           Property
---  -- ----                           --------
  0   1 1033                           {Install}
  0   6 PowerShellEngine               {ApplicationBase, ConsoleHostAssemblyName,PowerShellVersion,
                                        RuntimeVersion...}
  3   0 PowerShellSnapIns              {}
  1   1 PSConfigurationProviders       {(default)}
  1   0 ShellIds                       {}

PSH [HKLM:\SOFTWARE\MICROSOFT\PowerShell\1]: ls .\PowerShellSnapIns

    Hive: HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\PowerShell\1\PowerShellSnapIns

SKC  VC Name                           Property
---  -- ----                           --------
  0   7 PowerGUI                       {Version, PowerShellVersion, AssemblyName,ApplicationBase...}
  0   9 Pscx                           {PowerShellVersion, Vendor, Description, Version...}
  0  11 Quest.ActiveRoles.ADManagement {AssemblyName, Description, ModuleName, PowerShellVersion...}

This is pretty cool. And looking at the PowerShellSnapIns – this information is useful for managing snap-ins on clients and servers. You could test the existence of this Registry key as follows:

PSH [C:\foo]: Test-Path hklm:\SOFTWARE\MICROSOFT\PowerShell\1
True
PSH [C:\foo]: $PSVersionTable

Name                           Value
----                           -----
CLRVersion                     2.0.50727.3074
BuildVersion                   6.1.6949.0
PSVersion                      2.0
PSCompatibleVersions           {1.0, 2.0}

But testing the existance of this registry path from within PowerShell is somewhat meaningless. If PowerShell exists on a given system, then the key will exist so the test will of course succeed. But if PowerShell does NOT exist, you’d never be able to run the script in the first place so being able to test from within the PowerShell script is unhelpful. I suppose you could write a C# program or a VBScript script to do the detection.

But wouldn’t a better way be to just get PowerShell deployed? I’d base the wide deployment to down level systems on the RTM version of The Windows Management Framework. The WMF Release Candidate was posted to the web on Aug 13th, so is getting very close to release. I do not have any specific knowledge, but given past experience by the PowerShell team, I’e expect to see this by the end of the year, maybe at TechEd Berlin.

Technorati Tags:

2 comments:

jv said...

Try this:

function Test-Powershell(
[string]$computername = $(Throw "Must supply host name")
){

$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $computername)
$result = $reg.OpenSubKey("SOFTWARE\MICROSOFT\PowerShell\1")
if($result){$true}else{$false}
}

Per Østergaard said...

From CMD it is easy to check for PowerShell:

reg query hklm\SOFTWARE\MICROSOFT\PowerShell\1 /v "" | find "power" || echo nops

V2 seems to create the PSModulePath environment variable, so that makes it even easier:

If defined PSModulePath blah