Skip to content

Instantly share code, notes, and snippets.

@techthoughts2
Last active November 12, 2017 04:46
Show Gist options
  • Save techthoughts2/996de424ad22e1f5a8de to your computer and use it in GitHub Desktop.
Save techthoughts2/996de424ad22e1f5a8de to your computer and use it in GitHub Desktop.
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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment