Home > Apps, Powershell, Scripting > Function : Get-Function

Function : Get-Function

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

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: