PowerShell function for registry value test

Below is a useful function for checking the existence and the value of a registry key.
Might need some rework, it’s not in a final version

function Test-RegistryValue
{
  param(
    [Parameter(Mandatory=$true)] [string]$Path,
    [Parameter(Mandatory=$true)] [string]$Name,
    [Parameter(Mandatory=$false)] $KeyValue
  )

  if( -not (Test-Path -Path $Path -PathType Container) ) {
    return $false
  }

  $properties = Get-ItemProperty -Path $Path
  if( -not $properties ) {
    return $false
  }
  else {
    $value = ($properties | Select-Object -ExpandProperty $Name)
    if ($value.ToString().ToLower() -eq $KeyValue.ToString().ToLower()) {
      return $true
    }
    else {
      return $false
    }
  }

  $member = Get-Member -InputObject $properties -Name $Name
  if( $member ) {
    return $true
  }
  else {
    return $false
  }
}

Note: Credits go to StackOverflow, IT blogs and alike, sites that helped me with the code when searching for solutions.

Advertisements

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: