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

Comments

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

    ReplyDelete

Post a Comment

Popular posts from this blog

SOLVED: Exchange Online Management PowerShell Connect-ExchangeOnline bug "A window handle must be configured. See https://aka.ms/msal-net-wam#parent-window-handles"

System error 86 has occurred. The specified network password is not correct.

The security identifier of the "NT SERVICE\WdiServiceHost" account is "S-1-5-80-3139157870-2983391045-3678747466-658725712-1809340420"