Windows – How to we check PowerShell v2.0 is installed on a list of servers

powershellwindows

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.

Best Answer

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.