Convert string to hex for registry

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 6 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Convert string to hex for registry

Post by jvierra »

Example:
  1. D:\scripts> [System.Text.Encoding]::Unicode.GetBytes('12345')|%{[System.Convert]::ToString($_,16)
  2. 31
  3. 0
  4. 32
  5. 0
  6. 33
  7. 0
  8. 34
  9. 0
  10. 35
  11. 0
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Convert string to hex for registry

Post by jvierra »

Considering you want to store this in the registry then this is all you need to do.

$bytes = [System.Text.Encoding]::Unicode.GetBytes('12345')
New-ItemProperty -PAth HKCU:\SOFTWARE\TestKey1 -Name Test1 -Value $bytes -PropertyType BINARY
regbin.png
regbin.png (18.98 KiB) Viewed 3202 times
User avatar
syscouk89
Posts: 56
Last visit: Fri Aug 25, 2023 8:25 am

Re: Convert string to hex for registry

Post by syscouk89 »

Ha, thank you that's working exactly as you said. I think because I ran just the first bit without writing to the registry I assumed it would write the same as what it outputted. Obviously after writing to the registry it is correct.

Thanks again!
User avatar
syscouk89
Posts: 56
Last visit: Fri Aug 25, 2023 8:25 am

Re: Convert string to hex for registry

Post by syscouk89 »

One more question, how do I overwrite the value? If I change it to set-itemproperty then "ProperyType" for BINARY isn't available

EDIT: Never mind, just used -force
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Convert string to hex for registry

Post by jvierra »

syscouk wrote:EDIT: When I run [System.Text.Encoding]::Unicode.GetBytes('0.1.1.')|%{[System.Convert]::ToString($_,16)} I get 30 0 2e 0 31 0 2e 0 31 0 2e 0 which is wrong for the printer
The registry does not have any dots. It is just how regedit displays an array of bytes.

To see the raw value use REG.

reg query HKCU\Software\TestKey1 /v test1
User avatar
syscouk89
Posts: 56
Last visit: Fri Aug 25, 2023 8:25 am

Re: Convert string to hex for registry

Post by syscouk89 »

Fair enough, thanks again worked a treat once I'd got over my own understanding!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Convert string to hex for registry

Post by jvierra »

Note that you can also just do this:

reg add HKCU\Test /v test1 /t REG_BINARY /d 003000310031 /f

Adding a property to the registry will also create the key if it does not exist.
This topic is 6 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked