5/8/10

PowerShell Registry Script

Four variations for reading registry keys.
HKLM and HKCU are PowerShell drives for HKEY_LOCAL_MACHINE and HKEY_CURRENT_USER registry hives.

Windows startup entries.
getRegistryV1 hklm:\software\microsoft\windows\currentversion\run
getRegistryV1 hkcu:\software\microsoft\windows\currentversion\run
function getRegistryV1($key)
{
  $key = get-item $key
  $values = get-itemProperty $key.psPath  #gp alias for get-itemProperty
  $values
}

function getRegistryV2($key)
{
  $key = get-item $key
  $values = get-itemProperty $key.psPath
  foreach ($value in $key.property)
  {
    "$value = $($values.$value)"  #subexpression $()
  }
}

function getRegistryV3($key)
{
  $key = get-item $key
  $maxKeyNameLen = ($key.property | %{$_.length} | measure -max).maximum
  $values = get-itemProperty $key.psPath
  foreach ($value in $key.property)
  {
    "{0,-$maxKeyNameLen} = {1}" -f $value, $values.$value  #format left-aligned width
  }
}

function getRegistryV4($key)
{
  $key = get-item $key
  $maxKeyNameLen = ($key.property | %{$_.length} | measure -max).maximum
  $values = get-itemProperty $key.psPath
  $key.property | %{"{0,-$maxKeyNameLen} = {1}" -f $_, $values.$_}  #format left-aligned width
}

No comments:

Post a Comment