简体   繁体   中英

How do I change a REG_DWORD value's base from Hexadecimal to Decimal via PowerShell cmdlets?

For a private preview tool provided by Microsoft, a registry value needs to be set to Type REG_DWORD value 46, then change the Base from hexadecimal to decimal. Here is a portion of the documentation I am referring to:

  1. Use the Edit menu or right-click to create a new DWORD (32-bit) Value and name it WUfBDF (note the only lowercase letter in this name is the 3rd 'f' and all the rest are uppercase).
  2. Next, right-click on the new value and select the Modify… option.
    Make sure to choose the Decimal base and set the value to 46.

I am creating a Proactive remediation script to push out to a group of machines that need this Reg key/item for the preview tool to work.

$regkeyPath = "HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate"
$regEntry = "WUfBDF"
$desiredValue = 46

function createRegEntry($path, $entry, $value){
    write-output "Remediating registry entry."
    if(test-path -Path $path){
        write-output "$path exists. Setting $entry"
        Set-ItemProperty -Path $path -Name $entry -Value $value -Type DWord -force | out-null
    }else{
        New-Item -Path $Path -Force
        New-ItemProperty -Path $path -Name $entry -Value $value -PropertyType DWord -force | out-null
    }
}

createRegEntry $regkeyPath $regEntry $desiredValue

I read the Set-ItemProperty documentation here from Microsoft Documentation and it seems that when creating a REG-DWORD value its Base defaults to hex and has to manually be altered. Any way to change it to REG_DWORD with the Base as Decimal

The "base" option in the regedit UI is a convenience for the user. The instructions could just as easily have said "set base to Hexadecimal and enter 2e" - they're both the same number. Sometimes it's just easier to work with a hex number - eg 0xF000 (hex) instead of 61440 (decimal) or vice versa.

In fact, if you set the dialog to Decimal, enter 46 and then change to Hexadecimal you'll see the "Value data" change to 2e automatically...

在此处输入图像描述

在此处输入图像描述

And it shows both values in the main details pane:

在此处输入图像描述

Similarly, when writing your PowerShell code you could write $desiredValue = 46 or $desiredValue = 0x2E and get the same result. They're both representations of the same number - one decimal and one hexadecimal - it just depends on your preference which format you use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM