Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a binary registry value (REG_BINARY) with PowerShell?

Tags:

How to set a binary registry value (REG_BINARY) with PowerShell?

Background:

I need to change some properties of the ASP.NET State service using a PowerShell script. Unfortunately, the built-in PowerShell cmdlet Set-Service only lets you modify the service description, startup type, display name, and status. I need to modify the Subsequent failures property found on the Recovery tab (when viewing the service's properties). I found that this value was stored in the registry as a REG_BINARY value.

An export of the value looks like this:

[HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\services\aspnet_state]
"FailureActions"=hex:50,33,01,00,00,00,00,00,00,00,00,00,03,00,00,00,0e,00,00,\
  00,01,00,00,00,00,00,00,00,01,00,00,00,00,00,00,00,01,00,00,00,00,00,00,00

In Powershell there is a Set-ItemProperty cmdlet with which you can set registry value values. For a string or dword value, you can just pass a string or an int. I know which hex value in the array to change, but I can't figure out how to set a binary value.

like image 413
brett rogers Avatar asked Jul 01 '11 17:07

brett rogers


People also ask

How would you set a registry value with PowerShell?

You can also use the New-ItemProperty cmdlet to create the registry entry and its value and then use Set-ItemProperty to change the value. For more information about the HKLM: drive, type Get-Help Get-PSDrive . For more information about how to use PowerShell to manage the registry, type Get-Help Registry .

How can I change binary code in registry?

On the Edit menu, point to New, and click Key; then, type the name of the subkey. Select the subkey under which you want to create a new value. On the Edit menu, point to New and then click String Value, Binary Value, or DWORD Value. Type the name of your new value.

How do I read a registry value in PowerShell?

One of the easiest ways to find registry keys and values is using the Get-ChildItem cmdlet. This uses PowerShell to get a registry value and more by enumerating items in PowerShell drives. In this case, that PowerShell drive is the HKLM drive found by running Get-PSDrive .


2 Answers

The following line gives you an example how to create one

New-ItemProperty -Path . -Name Test -PropertyType Binary -Value ([byte[]](0x30,0x31,0xFF))

and how to change an existing one:

Set-ItemProperty -Path . -Name Test -Value ([byte[]](0x33,0x32,0xFF))
like image 77
Howard Avatar answered Oct 07 '22 20:10

Howard


Is it just me who feels this misses the main part of this question?

How would you go about changing the original:

50,33,01,00,00,00,00,00,00,00,00,00,03,00,00,00,0e,00,00,\
00,01,00,00,00,00,00,00,00,01,00,00,00,00,00,00,00,01,00,00,00,00,00,00,00

Into a format like:

([byte[]](0x33,0x32,0xFF))

EDIT: After trying to get this working it turns out you just prefix all of the pairs with '0x'. Not sure why that was not mentioned in the answer. So just change the above to:

0x50,0x33,0x01,0x00,0x00,0x00,0x00,0x00... etc.

Then wrap that in the following:

([byte[]](0x50,0x33,0x01,0x00,0x00,0x00,0x00,0x00... etc.))
like image 40
xBr0k3n Avatar answered Oct 07 '22 20:10

xBr0k3n