Monday, 13 November 2017

Read a REG_EXPAND_SZ value from the registry using PowerShell

The Get-Item PowerShell cmdlet is nice and easy to use to get a registry key or value. Here's how we get a key.

$key = Get-Item -Path "Registry::HKEY_LOCAL_MACHINE\Software\TestKey"

But if you want to get the value type, or the raw unexpanded REG_EXPAND_SZ value things get a bit awkward as the Get-Item cmdlet does not return this information. Luckily the registry key that is returned is actually a Microsoft.Win32.RegistryKey object so you can use the following method of that class.

$key.GetValueKind("New Value #6")
Returns "ExpandedString"

You can also read the raw value of a REG_EXPAND_SZ value using the following


$key .GetValue("New Value #6", [System.String]::Empty [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames

1 comment:

  1. Thanks for the information. Since [System.String]::Empty -eq '' you could replace it accordingly.

    ReplyDelete