# #PowershellBasics: Working with arrays

2November 25, 2019 by Kenneth Fisher

I mentioned arrays in a previous post so I figured I should talk about what they are and how to work with them. Now, I’m going to assume that as a DBA you may not really know what an array is (although you probably do) so here is a quick definition:

An array is a data structure that is designed to store a collection of items. The items can be the same type or different types.

Source

An array can have one or more dimensions (a column, a box, a cube etc) and can have a fixed data type or a not. If you don’t have a fixed data type then each element can any type of data. A integer, a string, a horse, etc. Well, maybe not a horse but it can contain any type of coding object. I tend to think of the fixed data type arrays as tables. A single dimension is a single column table, a two dimension table is multi column table, etc. Arrays without a fixed data type are more like excel spreadsheets.

Working with arrays is pretty easy. You declare them with **[]**s or by just assigning a list of elements to a variable. If you are declaring them using **[]**s then one pair will make a single dimension array, two pairs will make a two dimension array etc.

Here are a couple of examples:

[string[]]$TestArray = "Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine" #Or $TestArray = "Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"

[string[][]]$TestArray2 = ("Zero,Zero","Zero,One","Zero,Two","Zero,Three")` ,("One,Zero","One,One","One,Two","One,Three")` ,("Two,Zero","Two,One","Two,Two","Two,Three")` ,("Three,Zero","Three,One","Three,Two","Three,Three") #Or $TestArray2 = ("Zero,Zero","Zero,One","Zero,Two","Zero,Three")` ,("One,Zero","One,One","One,Two","One,Three")` ,("Two,Zero","Two,One","Two,Two","Two,Three")` ,("Three,Zero","Three,One","Three,Two","Three,Three")

At this point I should probably point out that arrays are 0 based. By that I mean the first element of an array is the 0 element not the 1 element. A bit confusing if you ask me but you’ll get used to it. Now, to reference an element you just have to put the position number in the **[]**s. The strings I put in the arrays are there so that it’s really obvious that I’m getting back the right values.

$TestArray[2] Two $TestArray[7] Seven $TestArray2[2][1] Two, One $TestArray2[0][3] Zero, Three

Now here’s where it gets interesting. This is Powershell, and one of the things that makes Powershell so .. well .. Powerfull .. is that you can pass multiple elements to a command and it will process it for each of them. So to do that we need to be able to get multiple elements back.

All elements

$TestArray2 Zero,Zero Zero,One Zero,Two Zero,Three One,Zero One,One One,Two One,Three Two,Zero Two,One Two,Two Two,Three Three,Zero Three,One Three,Two Three,Three

Multiple elements in a row

$TestArray[3..7] Three Four Five Six Seven

Multiple elements that aren’t in a row

$TestArray2[0,2][1] Zero, One Two, One $TestArray2[0][1,3] Zero, One Zero, Three

Where this got really weird is that I couldn’t figure out how to pull multiple elements from both parts of the array at once. For example this didn’t return what I expected.

$TestArray2[0,2][1,3] Two, Zero Two, One Two, Two Two, Three

Obviously I’m still pretty new at this so if I’m missing something or I’m flat out wrong somewhere feel free to make a note in the comments below and I’ll update the post 🙂

So in your last example what is occurring here is that the first index is selecting two array rows, the first and third as specified by [0,2] and returning that as a 1d array containing two items (each row from the 2d array becomes an item in this new 1d array), which your second index is acting upon to return the second and fourth item (row) as specified by [1,3], since there are only two rows in the resulting array it returns the second item and ignores the request for the fourth item.

This is confusing to follow since PowerShell is displaying each element in the generated 1d array as 4 lines each, plus each of your original items from your 2d array contained commas which further muddies the waters.

You can check this by trying things such as:

$TestArray2[0,2].Count #which will return:

2

$TestArray2[0,2][0] #which will return:

Zero,Zero

Zero,One

Zero,Two

Zero,Three

$TestArray2[0,2][0].Count #which will return:

1

Welcome to the wonderful world of PowerShell.

Still not sure how you managed to get this result though:

$TestArray2[0,2][1]

Zero, One

Two, One

In order to get what I believe you were going for in your last example you’d need to do something like so:

[string[][]] @($TestArray2[0][1,3],$TestArray2[2][1,3]) #which will return

Zero,One

Zero,Three

Two,One

Two,Three