I was wondering if there was a shorter way to create a multidimentional hashtable array in powershell. I have been able to successfully create them with a couple of lines like so.
$arr = @{}
$arr["David"] = @{}
$arr["David"]["TSHIRTS"] = @{}
$arr["David"]["TSHIRTS"]["SIZE"] = "M"
That said I was wondering if there was any way to shorten this to something like this...
$new_arr["Level1"]["Level2"]["Level3"] = "Test"
Where it would create a new level if it doesn't already exist. Thanks!
Working with arrays is a fairly straightforward process, but what a lot of people don't realize is that PowerShell does not limit you to using basic, run-of-the-mill arrays. You can also create multi-dimensional arrays.
GetEnumerator. A hash table is a single PowerShell object, to sort, filter or work with the pipeline you can unwrap this object into it's individual elements with the GetEnumerator() method.
An empty PowerShell hash table is created using @{}. PowerShell hash table values are added using $table. Add(key, value) syntax or $table. key = value syntax.
You need to name/define the sub-levels or else the interpreter doesn't know what kind of type it's working with (array, single node, object, etc.)
$arr = @{
David = @{
TSHIRTS = @{
SIZE = 'M'
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With