Help Creating Binary registry entries using an input file

I’m new to PowerShell, so the answer may be staring me in the face…
I’m building a script to modify various registry entries using a .csv as an input file.
The environment is a Win 7 environment, with PowerShell 3.0…

I’m stuck on properly creating the values for binary entries.
The section of script is simply adding sites to the “Allowed Sites” list for the pop-up blocker in IE 10 and/or 11 (GPOs are not an option).

Here’s the code-everything seems to work, except that the value is not as expected…

#Create an array using a .csv file that is formatted and populated with data as indicated below


$as = @(import-csv ‘somepath\Allowed.csv’)
foreach ($a in $as)
new-itemproperty -path ‘HKCU:\software\Microsoft\Internet Explorer\New Windows\Allow’ -name $ -type $a.type -Value $a.value

Running the script against the input file, I consistently get a registry value of {0}, rather than {0,0}. However, if I run the new-itemproprty cmdlet with the -value property discretely entered as 00,00…the value returns {0,0}.
i.e. “new-itemproperty -path ‘HKCU:\software\Microsoft\Internet Explorer\New Windows\Allow’ -name $ -type *a.type -Value 00,00” returns values as expected…

I presume my error lies in the properties of the .value noteproperty string value? I’d like to expand the base script to additional registry keys, so I need to make sure I get values to populate properly from the array properties. Am I missing a blatant conversion step, or is my issue more subtle?

Thanks for any help!

Charlie O

Can you manually add that value with no problem?

I’ve found an example format of what binary values should look like:

#Create an array using a .csv file that is formatted and populated with data as indicated below
# Name,Type,Value
$as = @(import-csv 'somepath\Allowed.csv')
foreach ($a in $as)
$Name = $a.Name
$Type = $a.Type
$Value = $a.Value
new-itemproperty -path 'HKCU:\software\Microsoft\Internet Explorer\New Windows\Allow' -name $Name -type $Type -Value "$Value"

The issue is due to the data type from the csv file. The binary data is being stored as a string and when you write the value to the registry key the sting is converted to a byte data type.

$a = "00,00"
$a #displays as 00,00
[byte]$a #displays as 0

to properly write the value from the csv you will need to convert $a.Value from a string to a CharArray

$as = @(import-csv 'PATHTO\Allowed.csv')
foreach ($a in $as)
new-itemproperty -path 'HKCU:\software\Microsoft\Internet Explorer\New Windows\Allow' -name $ -type $a.type -Value $a.value.ToCharArray()

also take a look at for more examples of working with binary registry keys

Hi Steve…
I can create the value directly with the cmdlet…
I’ve seen a couple of different iterations of the example you found (some with parentheses as yours shows, others with bracket) and they all throw errors, bracketed attempts give positional parameter errors or improper value format errors, depending upon how it’s bracketed. Those enclosed in parentheses throw expression errors.

I tried your version and it does not throw an error with the input file values formatted as I’ve shown in the top of my post, but the value still only writes as {0} (but this at least indicates that the iteration you found is the correct syntax).
If I change the values in my input file to “0x00,0x00”-the script throws "Cannot convert value “0x00,0x00” to type “System.Byte.” Error: “Additional non-parsable characters are at the end of the string.”

If I run the cmdlet manually with the value as indicated in your example (…-Value ([byte[]] (0x00,0x00))…), everything works fine. Including the value as (00,00) also works if entered in the example format.
Additionally, simply including the value as 00,00 with type binary without the [byte[]] statements also works. In all three cases, running the command manually returns {0, 0} for the value (perfectly formatted binary value)…

Truly puzzling to me at this point…

Hi Charles,

Jonathan actually gave you a far more useful answer than me, and taught me something in reading it myself.

Just do some trial an error, for example, try swapping out $value1, $value2 and $value3. I’ve never attempted what you’re attempting, but just play around with it :slight_smile:

“0x00,0x00”-the script throws "Cannot convert value “0x00,0x00” to type "System.Byte. tells me this is all along the right lines…

Please let me know what worked!

#Create an array using a .csv file that is formatted and populated with data as indicated below
# Name,Type,Value
$as = @(import-csv 'somepath\Allowed.csv')
foreach ($a in $as)
$Name = $a.Name
$Type = $a.Type
$StringValue = $a.Value
$Value1 = [byte]$StringValue
$Value2 = $StringValue.ToCharArray()
$Value3 = [byte]$StringValue.ToCharArray()

new-itemproperty -path 'HKCU:\software\Microsoft\Internet Explorer\New Windows\Allow' -name $Name -type $Type -Value $Value1

OK…still puzzled here…
Thanks for the input Jonathan…Your cause is pretty much what I suspected…
I’m guessing my remaining issues are with translation and my (lack) of knowledge therein,
The csv has a value of 00,00 (in quotes to match specs for csv)
Using ToCharArray for $a.value yields:
Script output of {48, 48, 44, 48…}-matching the decimal character for the Hex characters appearing in the registry
Registry setting of 30 30 2c 30 30- matching the hex character to decimal? output in the script
instead of script output of {0, 0}
Registry setting of 00 00

You can store the string value in your csv and use your code to convert to the binary value

# Name,Type,Value
$as = @(import-csv 'd:\test\test.csv')
foreach ($a in $as){
    #This assumes the csv file is encoded as ASCII
    $new = [System.Text.Encoding]::ASCII.GetBytes($a.Value)
    # If the file is encoded as unicode use the below line 
    # $new = [System.Text.Encoding]::UniCode.GetBytes($a.Value)
    # [System.Text.Encoding]::UniCode.GetString($new)

Here is some helpful info about what you see when you look at the registry key

Still no love using any of the methods…
It appears that if the input to -Value is evaluated to a single instance of 00,00, it is output as a single 0. Most frustrating!

I did find a workaround for the specific case of 00,00 as the input for a binary registry value that should output to 00 00 in the registry…
I simply added a column to my csv and separated the instances of 00…
Now the csv looks like this:
Now, new-property line looks like:
new-itemproperty -path ‘HKCU:\software\Microsoft\Internet Explorer\New Windows\Allow’ -name $ -Value $a.value1,$a.value2 -type $a.type
Script output is:
*.test1.cso : {0, 0} (as desired)
Registry output is
*.test1.cso Reg_Binary 00 00 (as desired)

I know this is a limited solution, so please, any additional info would be appreciated and I’ll keep digging as well…


Are you sure you’re writing out your binary in a valid format?

Here’s what I would do:
Test out some example strings you find on the internet, For Example “(0x00,0xFF)”. Put it in a variable and try to convert it, once you’ve converted it successfully and pipe it to get-member to see if it’s what you want.

    $test = "(0x00,0xFF)"
    $new = [System.Text.Encoding]::ASCII.GetBytes($test)
    # If the file is encoded as unicode use the below line 
    # $new = [System.Text.Encoding]::UniCode.GetBytes($test)
    $new | gm

Then, use the string you’re trying to use, does it convert? does it work as expected?

I don’t have a direct answer for you, but hopefully if this is helpful you’ll come back and tell me how it’s done :slight_smile:

Thanks Stephen…
It’s very possible that my original premise is incorrect…I’m not the brightest candle in the room working with character sets :slight_smile:
I’ll definitely spend some more time researching (and testing your example)… :slight_smile:
I know that the workaround I have is limited and doesn’t give the flexibility needed for anything other than this specific use case, so I’ll need to work on it more…

The big key in the long run will be writing something that I can give a tech such that he/she can simply provide the base information to the input file (reg keys, value name, value type, value) in common formats and get the script to do the interpretation correctly across a range of input types (that will take a little figuring out…:slight_smile: ). For now, I’ve got a short-term solution that can be deployed for the point problem and similar problems that will give me the breathing room to build a more complete and correct long-term solution…


Old post.

Be that as it may.

The Value column in the CSV file is not properly formatted for the registry cmdlet -Value parameter. You must convert the CSV Value from the string data type e.g. “00, AA” to an array of byte data types e.g. 0, 170.


$valueArray = “00,AA” -split ‘,’

This creates a two-element array with the data stored as ‘00’, ‘AA’.

Convert the data in $valueArray to a known number base, hexadecimal in this case AND store them as byte data types using casting:

$byteArray = $valueArray | ForEach-Object { [byte]$_.Insert(0,‘0x’) }

The $byteArray now contains the bytes ‘0’, ‘170’. Provide this variable as the data for the registry cmdlet parameter -Value $byteArray

To summarize and fancify:

$valueArray = ‘C0,DE,10,00,10’

$binaryValue=[byte[]](($valueArray -split ‘,’) | ForEach-Object { “0x$_” })