techthoughts2
12/28/2015 - 4:12 PM

Test Registry Key

Tests to determine if specified registry key is present

<#
.Synopsis
   Tests if specified registry key is present
.DESCRIPTION
   Evaluates provided registry path and determines if key is present. True or false is returned.
.PARAMETER RegKeyPath
   Registry key path - must be in PS shorthand. Ex: HKLM:\SECURITY
.EXAMPLE
   Test-RegKey -RegKeyPath HKLM:\SECURITY
   Verifies if the specified registry key exists
.EXAMPLE
   Test-RegKey HKLM:\SECURITY
   Verifies if the specified registry key exists - the -RegKeyPath paramater does not have to be explicitly used
.OUTPUTS
   Boolean value
.NOTES
   Author: Jake Morrison
   http://techthoughts.info

   PS Shorthand reg keys must be used
   ACCEPTABLE: HKLM:\SECURITY
   NOT ACCEPTABLE: HKEY_LOCAL_MACHINE\SECURITY
#>
function Test-RegKey {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true,
            ValueFromPipeline = $true,
            Position = 0
        )]
        [String]
        $RegKeyPath
    )
    #assume the best
    [bool]$regEval = $true
    try {
        if (!(Test-Path -Path $RegKeyPath -ErrorAction Stop)) {
            $regEval = $false
        }
    }
    catch {
        Write-Output "An error was encountered checking the registry key:"
        Write-Error $_
    }
    return $regEval
}