windows Set-ItemProperty 在某些系统上将注册表值设置为字符串而不是双字,为什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16787365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 10:33:22  来源:igfitidea点击:

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

windowspowershellregistrydword

提问by Erik

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

我尝试Set-ItemProperty在 PowerShell 中创建一个项目,该项目适用于大多数系统:

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.

这会在大多数 Windows 7 系统上创建一个 DWORD 值,但我发现有一个系统会创建一个 STRING 值,我想知道:为什么?系统行为不同会发生什么?所有人都没有设置该值,所有人都使用相同的 Powershell 版本使用相同的基本映像。

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.

但出于好奇,我想知道为什么这些系统的行为不同。

回答by Shay Levy

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 RegistryValueKindvalue (you can also use it with New-ItemProperty) :

我不知道为什么会发生这种情况,但要避免这种情况,请明确说明。使用 Type(动态)参数并指定RegistryValueKind值(您也可以将它与 一起使用New-ItemProperty):

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

回答by gabriwinter

Try this.

尝试这个。

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