7

How can I check if PowerShell v2.0 is installed on a list of servers? The list of servers is a combination of Windows Server 2003 and Windows Server 2008. A vbscript or PS script would be wonderful.

I know PowerShell is installed, by default, in Windows Server 2008, though I just like to make sure somebody was not messing with the servers.

2 Answers 2

2

After digging around and finding several places that lead to nothing usable in my environment, I was able to find this. This will work on systems that don't have Powershell, so it could be used for other registry scans.

$hostA = “RemoteComputer”
$cred = Get-Credential "domain\username"
$RegPath = "SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine"
$ValueName = "PowerShellVersion"
$tmp = $(gwmi -computername $hostA -class win32_service -Credential $cred | Where-Object { $_.Name -eq “RemoteRegistry” })
if ( $($tmp.State) -eq "Stopped") {$tmp.StartService()}
$reg = Get-WmiObject -credential $Cred -List -Namespace root\default -computername $hosta | Where-Object {$_.Name -eq "StdRegProv"}
$ref = ($reg.GetStringValue(2147483650,$RegPath,$ValueName)).sValue
if ($ref -eq $null ) {Write-Host $hostA "doesn't have Powershell"} else {Write-Host $hostA "has Powershell version" $ref}

Edit: After looking around, it turns out this won't work if the Remote Registry service isn't running. I added code to check for remote registry and start if stopped. The $cred = Get-Credential "domain\username" can be removed along with -Credential $cred depending on your domain setup.

Edit2: Ok, I have found permission issues with using .OpenSubKey(). I am switching it to .GetValueKind(), since all we need to do is verify the key exist. Hopefully this works for you. I don't have the permissions where I work to fully test this for you.

Edit3: I was having a lot of problems with permission in my environment since I cross untrusted domains. I have rewrote the above code to work in my environment. It works remotely, and doesn't require Powershell to be installed. Let me know if you get any other errors.

8
  • I tested on my PC first. I replaced "RemoteComputer" with "FQDN of my server", and it tells me my PC does not have PowerShell. I am not sure what the value of $ref is and how it works here.
    – whizkid
    Jul 12, 2012 at 16:04
  • Are you getting any other errors? I'm in a place I can actually test it, and I'm getting an error getting $regKey to set properly. I'll look into it and see what I can find.
    – Nick
    Jul 12, 2012 at 17:19
  • Error says: Exception calling "GetValueKind" with "1" argument(s): "The specified registry key does not exist." At D:\Whiz\MyScripts\findPS1.ps1:7 char:28 + $ref = $regKey.GetValueKind <<<< ("\SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine\PowerShellVersion") + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException ... though this script looks OK, looks like something about the way it calls the registry. I am still trying to figure this out.
    – whizkid
    Jul 13, 2012 at 14:47
  • I cannot test anything right now, but when I get back to work Monday, I'll see if I can figure out what is going wrong.
    – Nick
    Jul 15, 2012 at 15:55
  • I fixed the script above. It is working in my environment. Hopefully this will solve your problem.
    – Nick
    Jul 16, 2012 at 17:26
3

I'd check this key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine\powershellversion.

1
  • This is correct. However, with all due respect, I cannot mark this as the answer for the only reason that my query was to check multiple/list of machines.
    – whizkid
    Jul 13, 2012 at 15:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .