Archive

Posts Tagged ‘registry’

Function : Get-Function

February 24, 2011 Leave a comment

Here’s a time saver… well a quick function that has saved me a whole herd of time when trying to find a function that has been “forgotten”

Function Get-Function ($searchterm,[switch]$DEFINITION)
{
	$functions = @(Get-Command "*$searchterm*" -CommandType "Function")
	"`nFunctions matching ""$searchterm""`n" | Write-Host -ForegroundColor Green
	if (!$DEFINITION) {$functions | %{$_.name}} else {$functions | %{"`n$($_.name)" | Write-Host -f Yellow ;$_.definition}}
}

sal gf Get-Function

the last line is setting an alias “gf”
– so let’s try this on something familiar… we had a function searching for apps…

Command: gf apps
Output:
Functions matching “apps”
Get-Apps
Get-CoreApps
Get-InstalledApps
Set-InfsApps

Ahh – “installed apps”, let’s try that one again, and get Details this time:

Command: gf installedapps -de
Output:
Functions matching “installedapps”
Get-InstalledApps
param (
$computername=$env:computername,
$regkey_x64 = “SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall”,
$regkey_x86 = “SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall”,
[switch]$DISPLAY,
[switch]$EXCLUDE32,
[switch]$EXCLUDE64
)
$bits_to_search = @()
if (!$EXCLUDE32){$bits_to_search += “x86”}
if (!$EXCLUDE64){$bits_to_search += “x64”}

$apps = @()
foreach ($bit in $bits_to_search)
{
$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

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
}