Arrays - Zero is the loneliest number
In simple terms an array is an organized list of variables. Arrays are organized by index, as objects are added to the list they are automatically given the next whole number as their index. Arrays in most languages, including Powershell, are zero-indexed, which means that the first object in the array is at index zero and it goes up from there.
Creating an Array
There is more than one way to create an array in Powershell, all of which are valid and have their own benefits. You can create an array using @() and assigning the result to a variable, or you can list out your array with a comma separator. If you just need to populate an array with a range of numbers, you can use the range operator ..
Accessing an Array
Accessing an individual element of an array is done by using the variable name with the [] operator and the index.
Note that accessing the third element of the array requires using the number 2. This is because the array is zero indexed. Using 3 would return the fourth element of the array, this is called an Off-By-One Error.
An aside about Off-by-One
An Off-by-one error occurs when you don't get the results you were expecting from a program because somewhere in it, a number you used is off by one of what would return the correct results. An Off-by-One error commonly happens in two places, one is array indexes, the other is in loops, which we'll talk about when we talk about loops. In this case, it happens because of the difference in how humans think about lists, and how the language indexes the array.
If you ask most people in the US to count something, they're going to start counting at one. When counting it makes sense to skip over using zero when we can obviously see that there are more objects than that. This is where Off-by-one errors happen in arrays.
The index in an array was not originally designed as a counter of the number of objects in an array. In BCPL (which was a precursor to the language C) the index was designed as an offset. When you declared an array, you had to declare what was contained in the array (Unlike Powershell, you couldn't mix types. An array contained only ints, or only floats, or so on). If you declared an int array, the system knew that an int was a certain size, and so reserved enough space in memory for the array based on the size of the declared array. To access each individual element of the array, the system looks in memory at the address of the array, plus an offset of the index multiplied by the size of the type of the array.
Thinking of the index as an offset rather than a count will help you from making too many Off-by-one errors, at least where arrays are involved. Back to arrays.
Accessing an Array - Part 2
There are a number of unique ways to access an array in Powershell when you want to access multiple elements, or perhaps work backwards through an array.
If you need a certain number of elements from the array, you can access them by separating them with a comma in between the [] operator.
You can also access multiple elements by using the range operator .. .
If you need to access the last element of an array you can use some built in Powershell instance variables to do it ( .count ) or, you can use negative numbers.
You can see how using negative number results in much cleaner and easier to read code, provided you know how the negative numbers work with arrays. $Array.Count returns the count of elements in the array, not the last index, so you have to subtract one from the count to get the last index. Otherwise, you're going to have an Off-by-one error.
Updating an Array
Updating a single element in an array is easy, just set that element to it's new value using the assignment operator = . Trying to assign something to an index that is outside of the range of the array will result in an error however.
Adding new elements to an Array
You can't.
Once an array is declared, you can't change the size of it. But there are some work arounds for adding elements to an array.
If you have two arrays you can add them together using the + operator which creates a third array out of the first two.
To add a single element to an array, you can use the += operator. However, this doesn't simply add a new element to the array, it creates a new array combining the original array and the new element.
While this works on a small dataset it doesn't scale well. Which brings us to Lists.
Lists
Think of a List as a really fancy array. They're dynamic so you can add and remove elements as needed but otherwise function exactly the same as an array does.
Creating a List
Lists are part of a special namespace in Powershell, so you have to tell it where to look to use them. You can do this each time you declare a list, or you can do it at the start of your script with a bit of code. Lists are required to have a type, so you have to define what is contained in the list when you declare it. If you need to store multiple types or objects, you can use PSObject as the type.
Adding and Removing Elements from a List
Adding and removing elements from a list is done with the .add() and .remove() functions of the List. Adding an element always adds it to the end of the list. To remove an element, you have to pass that element in with .remove().
Multi-Dimensional Arrays
One thing you may run into or find a use for is a Multi-Dimensional array or nested array. You can nest as many arrays inside of each other as you want, however they get really hard to conceptualize after three. An easy way to visualize a two dimensional array (that is two layers of arrays) is a tic tac toe board or an excel file, for a three dimensional array visualize a Rubik's Cube. These functions pretty much identical to a normal array, just with extra sets of [] for each dimension.