Powershell – Arrays & Hash Tables

I don’t have too much to say about Arrays at this point, but as I’m partly blogging this for my own education, I’ll put down what I’m learning.  At this point, I’m expecting the arrays to be useful when trying to build my example project. One piece will be to step through existing files to look for certain flags or traits to tell me to do further processing.

Arrays

First of all, any variable that stores the results of a command with multi-line output will result in an array. This includes commands such as dir and ipconfig. Arrays start with position [0]. You can create an array by using comma-separated values such as $Variable = 1,2,3,4 or by using $Variable = @(1).

The first cool thing I saw was that using negative positions in an array steps through the array in reverse.  That means if I have an array of $Variable = @(0,1,2,3,4), that $Variable[1] will be 1.  $Variable[-1] will be 4.  I can see this being very useful when you need to step backwards through the results. This is very intuitive to me and I wish this were easier to implement in other scenarios. However, stepping through the array backwards could be tricky because it could result in creating a copy of the array. For small arrays this wouldn’t necessarily be a problem, but with large arrays, I could see performance becoming an issue. If you need to reverse the entire array, you can simply use [array]::Reverse($Variable) to reverse the array elements in place. (This actually changes the array stored in the variable.)

You can easily specify to return multiple elements from the array using $Variable[0,4,-12] which would return the 1st element, 4th element, and the element 12th from the end.

Adding an element is pretty easy. You can type:
  @Variable += @(NewElement)
However, this will create a new copy of the array in order to add another element because arrays in Powershell cannot be resized. This may be something to consider when working with large arrays.

Hash Tables

Hash tables seem to be pretty much what you’d expect. They store key/value pairs such as “Name”, “SQL Server 2008 Enterprise Edition” stored together. You define these in a manner very similar to that of arrays:
  $Variable = @{Name=”SQL Server”; Version=”10.0″; Edition=”Enterprise”}
That will define a new hash table containing 3 keypairs. Name, Version, and Edition.  Each will have a Name and Value property that will display when you display the variable. You can display just the value of a particular keypair with $Variable.Name or $Variable.Version.

Adding a new keypair is easy as well.  Use:
  $Variable.NewName = “New Value”

Similarly, you can remove a keypair with:
  $Variable.Remove(“Name”)

Hash Tables are used with the Format-Table command as well. You can customize the columns returned by manipulating their respective hash table values: Expression, Width, Label, and Alignment. Use get-help Format-Table for more information.

 

By default, copies of an array or hash table are actually copies of a pointer to the values. That means that if you just set the variable with the normal assign of $Var1 = $Var2, you’ll end up with pointers. Modifying either of those variables will affect the other(s). Modifying the array/hash table or using the .Clone() command on either will make a new copy.

Finally, if you need to strongly type an array, you can do so by putting the datatype before the array definition:
  [int[]]$MyArray = @(0,1,2,3)

This will ensure that all values stored in the array are of that datatype. Any attempts to add/remove values to the array that are not of that datatype will raise an error.

 

There are a lot of uses for arrays and hash tables. This just scratches the surface of what can be done, but it should be enough to get you started.

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