Last active
November 5, 2018 04:56
-
-
Save techthoughts2/04007c5eeb3497843bd869bb10fe2367 to your computer and use it in GitHub Desktop.
Retrieves the data value of a specified registry value name
This file contains 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 | |
Retrieves the data value of a specified registry value name | |
.DESCRIPTION | |
Returns the data contents of a registry keys value name specified by the user | |
.PARAMETER RegKeyPath | |
Registry key path - must be in PS shorthand. Ex: HKLM:\SECURITY | |
.PARAMETER Name | |
The value name to be checked. | |
.EXAMPLE | |
Get-RegistryValue -RegKeyPath HKLM:\DRIVERS\DriverDatabase -Name Version | |
Checks the value of Version in the specified registry key and returns the value | |
.EXAMPLE | |
Get-RegistryValue -RegKeyPath HKLM:\DRIVERS\DriverDatabase -Name Version -Verbose | |
Checks the value of Version in the specified registry key and returns the value with verbose output | |
.OUTPUTS | |
Data value of registry value in string | |
'value not set' if key has no data | |
null if key is not found | |
.NOTES | |
Author: Jake Morrison - @jakemorrison - http://techthoughts.info | |
Lots of various formats are stored in the registry but this function will convert those value and return a string | |
#> | |
function Get-RegistryValue { | |
param ( | |
[Parameter(Mandatory = $true, | |
Position = 0, | |
HelpMessage = 'Registry key path')] | |
[ValidateNotNull()] | |
[ValidateNotNullOrEmpty()] | |
[String] | |
$RegKeyPath, | |
[Parameter(Mandatory = $true, | |
Position = 1, | |
HelpMessage = 'Value Name')] | |
[ValidateNotNull()] | |
[ValidateNotNullOrEmpty()] | |
[String] | |
$Name | |
) | |
$result = $null | |
Write-Verbose -Message "Evaluating $RegKeyPath and pulling data from: $Name" | |
try { | |
$value = Get-ItemProperty -Path $RegKeyPath | Select-Object -ExpandProperty $Name -ErrorAction Stop | |
Write-Verbose -Message "Success." | |
Write-Verbose -Message "Value: $value" | |
#often - a registry value name may have no data. we will handle that here. null returns are only indicating errors | |
if ($null -eq $value -or $value -eq "") { | |
Write-Verbose -Message "Value was null, setting return to 'value not set'" | |
$value = "value not set" | |
} | |
#$value = Get-ItemProperty -Path $RegKeyPath -Name $Name -ErrorAction Stop | |
$result = $value | |
}#try_Get-ItemProperty | |
catch { | |
#do nothing - null will be returned | |
}#catch_Get-ItemProperty | |
return $result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment