PowerShell Registry Set-ItemProperty gotchas

Hi folks

Recently I wanted to set a registry value via PowerShell using

Set-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\Software\MyApp -Name MyKey -Value 1

If we take a look on his value regedit its type is REG_SZ

If the command above fails because it the node is not exist, let’s create it first

New-Item -Path Registry::HKEY_LOCAL_MACHINE\Software\MyApp

Let’s consider some questions:

How can I get registry value type using PowerShell?

Surprisingly Get-ItemProperty doesn’t contain any information for the value type

But this snippet works

(Get-Item -Path Registry::HKEY_LOCAL_MACHINE\SOFTWARE\MyApp).GetValueKind("MyKey")

and returns String

Obviously REG_SZ corresponds to String

How can we set registry value type other from REG_SZ?

Set-ItemProperty has one undocumented dynamic parameter Type

Set-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\Software\MyApp -Name MyKey -Value 1 -Type DWord

Interestingly, Type parameter appears in auto-complete only if current drive provider is Registry.

PS C:\> Set-ItemProperty -T<tab>

nothing happens

PS HKLM:\> Set-ItemProperty -T<tab>

expands to -Type

Alternatively we can use New-ItemProperty with documented PropertyType parameter

New-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\SOFTWARE\MyApp -Name MyKey -Value 1 -PropertyType DWord -Force

-Force parameter is required to override value if it already exists.

What is the list possible types

Do discover those types I tried

Set-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\Software\MyApp -Name MyKey -Value 1 -Type BlahBlah


Set-ItemProperty : Cannot bind parameter 'Type'. Cannot convert value "BlahBlah" to type "Microsoft.Win32.RegistryValueKind" due to invalid enumeration values. Specify one of the following enumeration values and try again. The possible enumeration values are "Unknown, String, ExpandString, Binary, DWord, MultiString, QWord".
At line:1 char:94
+ Set-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\Software\MyApp -Name MyKey -Value 1 -Type <<<<  BlahBlah
    + CategoryInfo          : InvalidArgument: (:) [Set-ItemProperty], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.SetItemPropertyCommand

This gives us a full list of possible values.

If we look at MSDN article for Microsoft.Win32.RegistryValueKind we find a full map

PowerShell type Registry type
ExpandString REG_EXPAND_SZ
MultiString REG_MULTI_SZ
String REG_SZ

About mnaoumov

Senior .NET Developer in Readify
This entry was posted in Uncategorized. Bookmark the permalink.

One Response to PowerShell Registry Set-ItemProperty gotchas

  1. sune hjort says:

    Great article. Helped me alot.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s