Skip to content

Instantly share code, notes, and snippets.

@jaredcatkinson
Created January 23, 2018 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jaredcatkinson/2a6a5a36c04e9a419cb3659acb2323f4 to your computer and use it in GitHub Desktop.
Save jaredcatkinson/2a6a5a36c04e9a419cb3659acb2323f4 to your computer and use it in GitHub Desktop.
function Get-RegistryValue
{
[CmdletBinding(DefaultParameterSetName = 'HKLM')]
param
(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[string[]]
$Key,
[Parameter()]
[string]
$ValueName,
[Parameter(Mandatory = $true, ParameterSetName = 'HKU')]
[string]
$SecurityIdentifier
)
process
{
foreach($k in $Key)
{
if((Get-WmiObject -Class Win32_ComputerSystem).SystemType -match 'x64')
{
$RegView = [Microsoft.Win32.RegistryView]::Registry64
}
else
{
$RegView = [Microsoft.Win32.RegistryView]::Registry32
}
if($PSCmdlet.ParameterSetName -eq 'HKLM')
{
$basekey = [Microsoft.Win32.RegistryKey]::OpenBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, $RegView)
$keyname = $k
$subKey = $basekey.OpenSubKey($k)
}
else
{
$basekey = [Microsoft.Win32.RegistryKey]::OpenBaseKey([Microsoft.Win32.RegistryHive]::Users, $RegView)
$keyname = "$($SecurityIdentifier)\$($k)"
$subKey = $basekey.OpenSubKey("$($SecurityIdentifier)\$($k)")
}
if($subKey)
{
foreach($value in ($subKey.GetValueNames()))
{
if($PSBoundParameters.ContainsKey('ValueName'))
{
if($value -eq $ValueName)
{
$props = @{
Key = "$($PSCmdlet.ParameterSetName)\$($keyname)"
ValueName = $value
Value = ($subKey.GetValue($value, $NULL, [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames))
}
New-Object -TypeName psobject -Property $props
}
}
else
{
$props = @{
Key = "$($PSCmdlet.ParameterSetName)\$($keyname)"
ValueName = $value
Value = ($subKey.GetValue($value, $NULL, [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames))
}
New-Object -TypeName psobject -Property $props
}
}
}
$basekey.Close()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment