How do I access properties of nested arrays in powershell within foreach loops?

119 Views Asked by At

I'm trying to write a script that checks whether a group of registry keys are set individually and then either sets them or modifies them conditional on thier current state.

The keys that I want to check/set contain a mix of Strings and DWORDs

I want to loop through an array that contains the key I want to set paired with the value.

I've tried as a hashtable/splatting but the input to Get-ItemProperty fails because of the value parameter so I tried basic arrays instead with no luck.

They are all at the same registry path but I was attempting to do something similar to this:

$Path = "HKLM:\Software\path\to\keys"

$Properties = (
     ('key', value),
     ('key2', value2),
     ('key3', 'value3')
)

foreach ($item in $Properties){
     $exist = Get-ItemProperty -Path $Path -name $item[0]

     if ($exist) {
          Set-ItemProperty -Path $Path -Name $item[0] -Value $item[1]
     } else {
          New-ItemProperty -Path $Path -Name $item[0] -Value $item[1]
     }
}

But no matter what I've tried I cannot retrieve the individual elements of the inner arrays.

I realize I could probably do this long-form and just do it line by line rather than attempting to iterate through, but this is definitely a more elegant way, and would be a great template if I need to do something similar in the future.

1

There are 1 best solutions below

0
Rokos_Modern_Basilisk On

Holy moly. Nothing breaks you out of a rut like posting the question to an online forum.

The .GetValue() method is what I needed, not the raw index number. ...So $Item.GetValue(0) for the key name and $Item.GetValue(1) for the value.

Feel silly answering my own question but hopefully it helps someone else!