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

Windows Server 2016, 2019, 2022, Windows 10 and Windows 11: Date and time "Some settings are managed by your organization".

TFTPD32 or TFTPD64 reports Bind error 10013 An attempt was made to access a socket in a way forbidden by its access permissions.

Enable function lock for F1-F12 on HP ZBook mobile workstations