PowerShell Power Story

I hadn't fully realized the power and simplicty of PowerShell until recently. My manager had asked if a script could be written that would take a CSV file of mailbox names, and hide them or unhide them. My co-worker, well-versed in VBScript, generated a 220 line solution that did just that. When I got into the office, I took that as an invitation to flex the PowerShell

Here is my code:

Import-csv Big-List-of-users.csv | %{get-mailbox $_.emailAddress | set-mailbox -HiddenFromAddressListsEnabled $true/$false}

Some nifty points:

  1. The value $_.emailAddress refers the the column in the CSV file of the same name.
  2. It's really easy to hide versus unhide a mailbox; simply change the $true to $false.

Note: This script utilizes the Exchange 2007 Management Shell, so you'll need to run this from a box with Exchange 2007 System Management tools on it. It does work with Exch2003 mailboxes.

Why such a difference? PowerShell has built-in functions that complete many of the basic tasks that you commonly need to do. For example the code:

import-csv (filename)

does quite a bit of work in only one bit. This reads the file, assigns variables based off the headers, and populates each record with the remainder of the file. Very nice function when you are looking to do a repetitive operation on a series of objects (like modify accounts, or query servers). In VBScript, the code would be something like:

set fso = createobject("scripting.filesystemobject")  ' create pointer to file system
set objFile = fso.opentextfile("filename.csv",1) ' create pointer to file

Set MyRSet = CreateObject("ADODB.Recordset") ' initialize a 2d array
If not objfile.atendofstream then 
     strFirstLine = objfile.readline  ' read first line to get headers
     arrFirstLine = split(strFirstLine,",") ' create array of header values
     For each strHeader in arrFirstLine  ' loop through array
          MyRSet.Fields.Append strHeader, adVarChar, 255 ' build records
     Next
     MyRSet.Open  ' open connection to data array created
     do while not objfile.atendofstream ' read file to populate array
          strCSVLine = objFile.readline
          arrCSVLine = split(strCSVLine,",")
          MyRSet.addnew Array(strFirstLine), arrCSVLine
     Loop
End if
set objFile = nothing

I know, the code is much more complicated than necessary for my initial project. It just goes to show how much more efficient PowerShell is for scripting. In VBScript, I would have likely done all my processing inside the file reading loop and never populated the array. My script would have been more tightly focused to that single purpose.

As I learn more about PowerShell, I become more, and more, fond of it. The versatility and simplicity are quickly winning me over and I find myself writing more PS scripts.

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <blockquote> <center> <hr> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options