Archive

Archive for the ‘Registry’ Category

Function : Get-InstalledApps

February 16, 2011 Leave a comment

So there I was happily using

gwmi win32_product 

to get me a list of installed applications, but this is slow, and often we just want a quick verification that an application is installed or not.

Thanks to powershell.com and their “PowerTip of the Day” I was reminded about hitting the registry for this information…
So, inspired by their code, I hacked together a quick function to do this.

Note the removal of duplicates was first done as per the concept here Script of the Day – remove duplicate lines in a CSV file but there were a few entries with special characters in their names, eg “C++” that don’t play nicely with “-match” so this was recoded comparing each entry of the object. This is going to be slower, but still solves the “Get Installed Applications” faster than the WMI route!

Function Get-InstalledApps 
{
	param (
		$computername=$env:computername,
		$regkey_x86 = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall",
		$regkey_x64 = "SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall",
		[switch]$DISPLAY
		)

	$apps = @()	
	foreach ($bit in "x86","x64")
	{	
		$regkey = Get-Variable "regkey_$bit" | %{$_.value}
		if (Test-Path HKLM:$regkey)
		{
			$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine",$computername)
			$key = $reg.OpenSubKey($regkey)
			$SubKeys = $key.GetSubKeyNames() | %{$key.OpenSubKey($_)}
	
			foreach ($subkey in $subkeys)
			{
				$app = $subkey | select @{N="Bit";E={$bit}},`
										@{N="Name";E={$_.getvalue('DisplayName')}},`
										@{N="Version";E={$_.GetValue('DisplayVersion')}},`
										@{N="Publisher";E={$_.GetValue('Publisher')}}
				if ($app.name) {$apps += $app}
				}
			}
		    $key.Close()
		    $reg.Close()
		}
   
	# Remove duplicates, but annoyingly names like of "C++" error when using "-match"
	$unique_apps = @()
	foreach ($app in $apps) 
	{
		if (!($unique_apps | ?{$_.Bit -eq $app.bit} | ?{$_.name -eq $app.name} | 
		?{$_.version -eq $app.version})) {$unique_apps += $app}
	}

return $unique_apps | sort bit,name,version
}