Last active
November 12, 2017 04:46
-
-
Save techthoughts2/996de424ad22e1f5a8de to your computer and use it in GitHub Desktop.
Tests to determine if specified registry key is present
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.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