An array in PowerShell is a data structure that is used to hold or store the collection of items. To create an array, first, add the “@()” and then the items in it separated by commas. Items in this collection can be of similar or distinct types. These items are stored at specific index locations. These index numbers help to call or index these items later. An array can be of multi-dimensions including 2D, 3D, or 4D.
In this post creation of multidimensional arrays will be discussed.
PowerShell Multidimensional Arrays
Multidimensional arrays can be created by simply adding arrays inside an array separated by commas. Examples to create multi-dimensional arrays are given below.
Example 1: Create a One-Dimensional Array
This instance will create a one-dimensional array:
$Array[1]
In the above-stated code:
-
- First, initialize a variable “$Array” and assign an array to it.
- After that, write the variable and specify the index number to display the item in the PowerShell output:
Example 2: Create a Two-Dimensional Array in PowerShell
In this example, the two-dimensional array will be created:
$2D_Array[1][1]
According to the above code:
-
- First, initialize a variable “$2D_Array”, then assign a two-dimensional array having two arrays separated by a comma.
- In order to access an item, simply write the two-dimensional array assigned variable.
- Then, invoke the element corresponding to the specified row and column via indexing:
Example 3: Create a Three-Dimensional Array in PowerShell
In this illustration, the three-dimensional array will be created:
$3D_Array[2][0]
Example 4: Create a Four-Dimensional Array in PowerShell
This example will demonstrate to create a four-dimensional array in PowerShell:
$4D_Array[3][1]
That was all about creating multidimensional arrays in PowerShell.
Conclusion
PowerShell multidimensional array is also known as an array of arrays. It can store items of different or of the same types. In a multidimensional array, each row has the same number of items. This post has elaborated on multidimensional arrays with the aid of several examples.