Home/PowerShell/How to Use Multidimensional arrays in Powershell

How to Use Multidimensional arrays in Powershell

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

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.

Leave a Reply