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
returns
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 |
---|---|
Binary | REG_BINARY |
DWord | REG_DWORD |
ExpandString | REG_EXPAND_SZ |
MultiString | REG_MULTI_SZ |
None | – |
QWord | REG_QWORD |
String | REG_SZ |
Unknown | – |
Great article. Helped me alot.