Before I show you how multi-dimensional arrays work, let we start what is an array. By default, PowerShell assumes that the elements of an array are of the type variant. This means that you can mix different data types—that is, combine numerical values, dates, or strings in a single variable. The data elements of a PowerShell array need not be of the same type, unless the data type is declared (strongly typed). However, if you think to restrict an array to a certain data type then you can declare the array as like below:
In simpler as like other languages, PowerShell arrays store one or more items. An item can be any data type like string, integer, another array or a generic object.
Multidimensional arrays are one of the complex data types supported by PowerShell. In simple let you imagine a multidimensional array like a table, with columns and rows, where each cell has its own index like [1,16). Each time we put a comma, we are like telling Powershell to start a new row in the multidimensional array
Here let we discuss about the Two dimensional array with example. As shown above, the Elements in two-dimensional arrays are commonly referred by x[i][j] where i is the row number and ‘j’ is the column number. A two-dimensional array can be seen as a table with ‘x’ rows and ‘y’ columns where the row number ranges from 0 to (x-1) and column number ranges from 0 to (y-1). A two – dimensional array ‘x’ with 3 rows and 3 columns is shown below:
Note:
The Powershell start a new row in the multidimensional array when we apply the comma in starting of new row as shown below.
$scoreDetails = @( @(‘Aadharsh’, ‘200’), @(‘Rakshu’, ‘199’) )
$scoreDetails+= ,(@(“Anitha”,150))
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
############################################################ #Project : How to Use Multidimensional arrays in Powershell #Developer : Thiyagu S (dotnet-helpers.com) #Tools : PowerShell 5.1.15063.1155 #E-Mail : mail2thiyaguji@gmail.com ############################################################ #For working with a large collection of items i would recommend using the ArrayList type from .NET as this is not a fixed size array so PowerShell will not destroy it every time you add an item to it and i've found this to work better in my projects. System.Collections.ArrayList]$scoreDetails = @() $scoreDetails = @( @('Aadharsh', '200'), @('Rakshu', '199') ) #Get user input and ADD to multi-dimensional array Write-Host "Enter NEXT person score details" $name = Read-Host "Name" $score = Read-host "Score" #Each time we put a comma, we are like telling Powershell to start a new row in the multidimensional array $scoreDetails+= ,(@($name,$score)) for($parentLoop=0; $parentLoop -lt 3; $parentLoop++) { for($childLoop=0; $childLoop -lt 2 ; $childLoop++) { "The value of [$parentLoop][$childLoop] ---> " +$scoreDetails[$parentLoop][$childLoop] } } |
OUTPUT:
What do you think?
I hope you have an idea of How to Use Multidimensional arrays in Powershell. I would like to have feedback from my posts readers. Your valuable feedback, question, or comments about this article are always welcome.
isn’t that a jagged array rather than a multidimensional array? (see get-help about_array)