Set-ItemProperty sets Registry Value as String on some systems instead of DWord, why?

左心房为你撑大大i 提交于 2019-11-30 00:49:52

问题


I try to create an item using Set-ItemProperty in PowerShell, which works on most systems:

New-PSDrive -name HKCR -PSProvider Registry -root HKEY_CLASSES_ROOT

Set-ItemProperty -Path HKCR:\Software\MyCompany\ -Name Level -Value 5 -ErrorAction SilentlyContinue 

This creates a DWORD-value on most Windows 7 systems, but I have found one system where this creates a STRING-value instead, and I want to know: why? What could happen that the systems behave differently? All don't have that value already set, all use the same base image using the same Powershell version.

Btw, I found that by using the following code, I can explicitly set a type, so I already solved the problem:

New-ItemProperty -Path HKCR:\Software\MyCompany\ -Name Level -Value 5 -ErrorAction SilentlyContinue -PropertyType DWord

But just for curiosity, I want to know why the systems behave differently.


回答1:


I don't have an answer to why it happens but to avoid such instances, be explicit. Use the Type (dynamic) Parameter and specify a RegistryValueKind value (you can also use it with New-ItemProperty) :

Set-ItemProperty -Path HKCR:\Software\MyCompany -Name Level -Value 5 -Type DWord



回答2:


Try this.

[Microsoft.Win32.Registry]::SetValue("HKEY_CLASSES_ROOT\Software\MyCompany","Level",5,[Microsoft.Win32.RegistryValueKind]::DWord)


来源:https://stackoverflow.com/questions/16787365/set-itemproperty-sets-registry-value-as-string-on-some-systems-instead-of-dword

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!