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

arr02

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

arr12

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

arr13

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

arr14

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

arr04

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

arr09

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() }

arr05

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

arr06

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)

arr07

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

arr08

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

arr10

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

arr11

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s