Working with Powershell arrays

In Powershell, arrays are one of the most useful data types. In a script they can be used to store and handle all sorts of data.

To declare an array:

$a = @()
# This declares an empty array

if ($a) { Remove-Variable a } # Make sure we're starting fresh
$a = 1..4
# Creates new array 'a', and assigns the numbers 1 through 4 to its first 4 elements
$a

http://superwidgets.files.wordpress.com/2014/08/arr02.jpg

We can also explicitly declare the variable type as in:

if ($a) { Remove-Variable a } # Make sure we're starting fresh
[array]$a = "fun"
$a.GetType() # This is an array, as opposed to:
Remove-Variable a # Make sure we're starting fresh
$a = "fun"
$a.GetType() # This is a string

http://superwidgets.files.wordpress.com/2014/08/arr12.jpg

Why does that matter? Although Powershell does a pretty amazing job at picking a data type for you when you don't explicitly declare a data type, sometimes you need to make sure you got the variable data type you want. For example:

if ($a) { Remove-Variable a } # Make sure we're starting fresh
$a = "fun"
$a += "sun"
$a

http://superwidgets.files.wordpress.com/2014/08/arr13.jpg

This may not be what we wanted, as now the 2 values are concatenated together into a single string.

This is more like it:

if ($a) { Remove-Variable a } # Make sure we're starting fresh
[array]$a = "fun"
$a += "sun"
$a

http://superwidgets.files.wordpress.com/2014/08/arr14.jpg

Array elements can be addressed by their index number between [] brackets. Index numbers start with zero. Array property 'count' tells us how many elements are in an array.

if ($a) { Remove-Variable a } # Make sure we're starting fresh
$a = 1..4
$a[1] # This is second element of the array, shows value of '2'
$a.count # This shows how many elements in the array => 4

http://superwidgets.files.wordpress.com/2014/08/arr04.jpg

We can also refer to several array elements by separating their index numbers with commas, like this:

if ($a) { Remove-Variable a } # Make sure we're starting fresh
$a = "Black","Blue","Cyan","DarkBlue","DarkCyan","DarkGray","DarkGreen","DarkMagenta","DarkRed","DarkYellow","Gray","Green","Magenta","Red","White","Yellow"
$a[2,7] # This selects the 2nd and 7th elements of the array - remember first element is number zero

http://superwidgets.files.wordpress.com/2014/08/arr092.jpg

Array elements can be of different data types:

if ($a) { Remove-Variable a } # Make sure we're starting fresh
$a = 1,2,3,4,"cheese",(Get-Date)
for ($i=0; $i -lt $a.Count; $i++) { $a[$i].GetType() }

http://superwidgets.files.wordpress.com/2014/08/arr05.jpg

To add an element to an array, we can use:

if ($a) { Remove-Variable a } # Make sure we're starting fresh
$a = 1,2,3,4,"cheese",(Get-Date)
$a += 7
$a += "Monday"
$a

http://superwidgets.files.wordpress.com/2014/08/arr06.jpg

Removing an array element is not straight forward. Although the array object has add() and remove() methods they do not work:

if ($a) { Remove-Variable a } # Make sure we're starting fresh
$a = 1..4
$a.Add(5)
$a.Remove(2)

http://superwidgets.files.wordpress.com/2014/08/arr07.jpg

This is due to the fact that Array implements the IsFixedSize property because it is required by the System.Collections.IList interface.

So, how can we remove an element from an array?

We simply create a new array excluding the element we want to remove, as in:

if ($a) { Remove-Variable a } # Make sure we're starting fresh
$a = 1..4
$a = $a -ne 2 # Creates new array based on the old 'a' array excluding the element whose value is '2'
$a

http://superwidgets.files.wordpress.com/2014/08/arr08.jpg

This also works for data types other than integer and string:

if ($a) { Remove-Variable a } # Make sure we're starting fresh
$Date = Get-Date
$a = 1,2,3,4,"cheese",$Date
$a = $a -ne $Date
$a

http://superwidgets.files.wordpress.com/2014/08/arr10.jpg

We can remove elements from an array based on their index number as well:

if ($a) { Remove-Variable a } # Make sure we're starting fresh
$a = "Black","Blue","Cyan","DarkBlue","DarkCyan","DarkGray","DarkGreen","DarkMagenta","DarkRed","DarkYellow","Gray","Green","Magenta","Red","White","Yellow"
$a = $a[0..3 + 5..$a.Count] # Removes the element with index #4 (DarkCyan) in the array
$a

http://superwidgets.files.wordpress.com/2014/08/arr11.jpg

 For more information see this link.