PowerShell Scripts and Useful Components

When writing long scripts, I’ve come to realize several helpful tips to keep the script useful, functional and explainable. In order for me to do this, I rely on comments, functions and arrays. I find these useful for many reasons. Hopefully this article will help explain what they are good for and encourage you to use them in your scripts:

Why use comments? I find comments useful because they can provide helpful information about the script and what a certain section is there for. It’s also useful for providing other people information on your script like the general purpose, version, change logs, etc.

What do comments look like in PowerShell:

Example 1


Example 2


In the first example the ‘#’ symbol just tells PowerShell not to process anything after that symbol on the same line. This is a one line only comment. If you need multiple lines, you could either keep using ‘#’ at the beginning of the line or see example 2. Example 2 uses the “<#" in-between those two symbols is where you place your comments. Again, it is a good place to put multiline comments or to start off a large script with a detailed explanation of the script (example 2).

What are functions? Functions are commonly used sets of PowerShell commands that can be reused in multiple parts of the script. So what does a function look like?

Example 3


The first line defines the name of the function being defined. In between the “{” “}” symbols is whatever PowerShell code you wish to evoke when the fucntion is called later in the script. In my case I am passing through a parameter (the ‘param’ line) so that I can resolve something related to the variable.

In my first long scripts, I exclusively used CSV files to store all data between steps in CSV files. This made for messy scripts that left files behind (if you forgot to remove them!). While it seemed more convenient to use CSV files, in reality Arrays allow more flexibility and better performance (no reading from disk to import CSV files). Arrays also allow you to create reusable data sets for multiple parts of a script. So how do we create and use an array:

Example 4 – define an array

$MyArray1 = @()

Example 5 – using an array

$servers = get-transportserver
$track = @()
$track2 = @()
foreach ($name in $servers) {
               $track = Get-MessageTrackingLog -server $name.name -EventId Expand -ResultSize Unlimited -start $onemonth -end $current
               $track2 += @($track)

Example 4 simply defines the variable $MyArray1 as a multidimensional array.

Example 5 is a bit more complicated. The script was written to look for a certain event in some Exchange 2010 Message Tracking Logs.

Line 1 gets all of your Exchange servers with the transport role installed.
Lines 2 and 3 define the multidimensional arrays.
Line 4 begins the loop for each Exchange Hub Transport server stored in $servers.
Line 5 looks for a certain event in the Exchange Message Tracking Logs and stores them in one array.
Line 6 stores those values in a new array, to be appended with each time through the loop.
Line 7 end or loops back to line 4

Once this data is captured by the arrays, it can be manipulated or reused later in your script.

Hopefull these tips will be useful the next time you are writing a script for Exchange, AD or whatever your need of the day is.
For Reference


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