PowerShell How-To
Working with Arrays in PowerShell
Learning how an array stores an object will help in using them in your code.
- By Adam Bertram
- 04/06/2017
PowerShell, like other programming languages, has different object types. These types are a way to define a kind of schema for how each object behaves. A few common types you may have heard of are strings, integers, hashtables and arrays. Each of these types is placeholders for storing things. Each type has a particular behavior. In this article, we're going to discuss how arrays work in PowerShell.
Similar to other languages, PowerShell arrays store one or more items. An item can be anything from a string, integer, another array or a generic object. Arrays can contain any combination of these items. Working with arrays primarily entails three actions. Those actions are adding items and removing items. Although some might argue that changing items is another action, as you'll see this is typically just an under-the-covers remove/add.
Each item in an array has an index. These are integers assigned to each item that represents its place in the array. Indices always start at 0 which means the first item in an array will always have an index of 0, followed by 1, 2, ad infinitum. These indices are how items are referred to in an array.
To demonstrate this, let's first define an array. Because PowerShell is built on top of the .NET Framework there are multiple ways to define arrays, but for simplicity, we're just going to focus on the most common way which is representing an array with an ampersand symbol followed by parentheses.
@()
You have created your first empty array. This is the structure of an array but as-is, it's not too useful. We must add some items into it. To add items to arrays, we can either do that when the array is created or after the fact. Let's say I want to store some fruits in my array. These will be represented as strings.
$fruit = @('Apples','Oranges','Bananas')
PS /Users/adam> $fruit = @('Apples','Oranges','Bananas')
PS /Users/adam> $fruit
Apples
Oranges
Bananas
You can see that our array is now represented by the $fruit variable and contains three strings. At this point, I could choose to add another string to the array by using the most common, short-hand method of destroying our array and creating another one all in one action.
PS /Users/adam> $fruit += 'Strawberries'
PS /Users/adam> $fruit
Apples
Oranges
Bananas
Strawberries
You can see above that I've "added" another string called Strawberries to our $fruit array by using the two operators, + and =. This seems like PowerShell is simply adding an item to the end of the array but in reality, it's destroying the array and creating a brand new one with the item appended. Adding items to an array is the most common process.
In the previous example, I've created an array of strings and could add other types of items inside. But what if I want to enforce only a particular type of array. In that case, I could explicitly cast the array to contain only certain types. I could do that including the type at the front of the array variable declaration.
[int[]]$numbers = 1,2,3,4,5
If I would then try to add a string to this array, I would receive an error.
PS /Users/adam> $numbers += 'ffff'
Cannot convert value "ffff" to type "System.Int32". Error: "Input string was
not in a correct format."
I'd now like to access all of the items inside of my array. I can do that using each item's indices. Notice that I can find how many items are in the array using the Count property and can reference each one using its index by enclosing each in brackets.
PS /Users/adam> $fruit.Count
4
PS /Users/adam> $fruit[0]
Apples
PS /Users/adam> $fruit[1]
Oranges
PS /Users/adam> $fruit[2]
Bananas
PS /Users/adam> $fruit[3]
Strawberries
Finally, let's remove the fourth item with the index of three. The array we've created is a "fixed" array, and we technically can't remove an item. Remember that I just referred to destroying and creating a new array? The same concept applies here. Perhaps I'd like to remove Apples from my array. To remove Apples from the $fruit array, I could enumerate over each item and exclude Apples like below.
$fruitNew = $fruit | Where-Object { $_ -ne 'Apples' }
Use arrays whenever you have the need to store a set of common items. Also, be aware that there are numerous other ways to manipulate arrays and other similar types that we could not possibly cover. For more information refer to the PowerShell About_Arrays help topic.
About the Author
Adam Bertram is a 20-year veteran of IT. He's an automation engineer, blogger, consultant, freelance writer, Pluralsight course author and content marketing advisor to multiple technology companies. Adam also founded the popular TechSnips e-learning platform. He mainly focuses on DevOps, system management and automation technologies, as well as various cloud platforms mostly in the Microsoft space. He is a Microsoft Cloud and Datacenter Management MVP who absorbs knowledge from the IT field and explains it in an easy-to-understand fashion. Catch up on Adam's articles at adamtheautomator.com, connect on LinkedIn or follow him on Twitter at @adbertram or the TechSnips Twitter account @techsnips_io.