Let's start off by looking at how arrays work in PowerShell. When working with arrays, you can access specific items and add or remove elements. In our first example, we assigned a list of server names to the $servers array. To view all of the items in the array, simply type the variable name and hit return:
[PS] C:\>$servers
EX1
EX2
EX3
Array indexing allows you to access a specific element of an array using its index number inside square brackets ([]). PowerShell arrays are zero-based, meaning that the first item in the array starts at index zero. For example, use the second index to access the third element of the array, as shown next:
[PS] C:\>$servers[2]
EX3
To assign a value to a specific element of the array, use the equals (=) assignment operator. We can change the value from the last example using following syntax:
[PS] C:\>$servers[2] = "EX4"
[PS] C:\>$servers[2]
EX4
Let's add another server to this array. To append a value, use the plus equals (+=) assignment operator as shown here:
[PS] C:\>$servers += "EX5"
[PS] C:\>$servers
EX1
EX2
EX4
EX5
To determine how many items are in an array, we can access the Count property to retrieve the total number of array elements:
[PS] C:\>$servers.Count
4
We can loop through each element in the array with the ForEach-Object cmdlet and display the value in a string:
$servers | ForEach-Object {"Server Name: $_"}
We can also check for a value in an array using the -Contains or -NotContains conditional operators:
[PS] C:\>$servers -contains "EX1"
True
In this example, we are working with a one-dimensional array, which is what you'll commonly be dealing with in the Exchange Management Shell. PowerShell supports more complex array types such as jagged and multidimensional arrays, but these are beyond the scope of what you'll need to know for the examples in this book.
Now that we've figured out how arrays work, let's take a closer look at hash tables. When viewing the output for a hash table, the items are returned in no particular order. You'll notice this when viewing the hash-table we created earlier:
[PS] C:\>$hashtable
Name Value
---- -----
server2 2
server3 3
server1 1
If you want to sort the hash table, you can call the GetEnumerator method and sort by the Value property:
[PS] C:\>$hashtable.GetEnumerator() | sort value
Name Value
---- -----
server1 1
server2 2
server3 3
Hash tables can be used when creating custom objects, or to provide a set of parameter names and values using parameter splitting. Instead of specifying parameter names one by one with a cmdlet, you can use a hash table with keys that match the parameter's names and their associated values will automatically be used for input:
$parameters = @{
Title = "Manager"
Department = "Sales"
Office = "Headquarters"
}
Set-User testuser @parameters
This command automatically populates the parameter values for Title, Department, and Office when running the Set-User cmdlet for the testuser mailbox.
For more details and examples for working with hash tables, run Get-Help about_Hash_Tables.