Exchange 2007 Scripting Corner: fix-alias
Published Jun 15 2007 02:04 PM 14.9K Views

One of the things that I have liked the most about the move to Exchange 2007 is the introduction of PowerShell into the management of Exchange. PowerShell is a very powerful tool that the Exchange administrator now has in their arsenal. Many regular, advanced and or mundane tasks can now be automated with PowerShell scripts or commands.

To help demonstrate what we can do with PowerShell, and get you the administrator interested in writing your own scripts, I hope to start posting a series of scripts (as I am able to write them) to the EHLO blog. The intent is that these scripts will solve a problem that you the Exchange administrators are having and will work as a way to teach PowerShell scripting thru example.

The first script I have for you is to solve a problem I have seen coming in on the phones here at Microsoft a few times. As our customers are migrating from Exchange 2003 to Exchange 2007 they are finding that some of the aliases of their contacts, users, or distributions groups contain invalid characters. If there are only a few it is simple to modify them by hand, but in many cases there are 10s if not hundreds that are incorrect.

With PowerShell and .net we can write a script that will correct this issue easily for us. Now most of the documentation of the script is in the comments sections of the script itself; but, I am going to go over the script flow and a few key things that I use in my script writing.

Script Flow:

At the bottom of the script your will find the Main body. This is actually where we begin processing.

  1. First we determine if the user has requested the help documentation using the –help input parameter.
  2. Next we check if the –type switch was passed otherwise we ask the user to choose and set the type of objects to look for.
  3. We construct the get command we will run into a variable (this is the best way to build "complex" commands to execute via multiple script inputs)
  4. We call and move into the FixObject Function
  5. The FixObject function is where the work is actually done
  • Execute the command we built and place the output in an array
  • Look thru each member of the array for our bad character
  • Replace the bad character when found (here we actually use a .net method from within PowerShell)
  • Build and execute the modification command

The true power of PowerShell I have found is in the ability to create functions. With a function you can encapsulate a given set of actions and then call those actions over and over again as needed in the rest of your script. In this script we use two functions ShowHelp; a function that appears in almost all of my scripts to provide the user with a "help" file on the script, and FixObject; the function that actually does all of the work in the script.

The other amazing power of PowerShell that we use in this script is the string.replace method. We are able to call that method directly from within PowerShell. Just to get an idea of the potential of PowerShell and .Net try this at a PowerShell prompt. Create a string Variable and set the value of the variable. (e.g. [string]$new = "Test" ). After setting our variable we will type the name of the variable followed by a period at the prompt. (e.g. $new.) Now simply press the tab key. What you will see is that you are now rotating thru the string methods available in .Net.

I would like to explicitly solicit feedback on this post. Did you find this ramble and the associated script helpful in showing some of the things that PowerShell can do and how to do them? Did this encourage you to write a script of your own? Also I would like to solicit Ideas from you on what you would like to see PowerShell do; and I will look into turning your Idea into my next posted script.

You can get the fix-alias script here: Fix-alias.PS1 script

- Matthew Byrd

Version history
Last update:
‎Jul 01 2019 03:28 PM
Updated by: