how to save your Exchange 2007 Powershell session transcript

Have you noticed that there are a lot more things that you need to do (or can do) via the Command Shell in Exchange 2007?  Do you wish that you could automatically save the session information to a text file?  Here's a great tip help you out with that.

First, a couple things.  In order to automate saving this information, you need to modify your PowerShell Profile.  If you aren't sure where your profile resides, at the command shell, simply type

[PS] C:\>$profile

This will return your profile information.  By default, it should be stored in the C:\Documents and Settings\<user>\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1.  However, if you look in that location, odds are you won't find anything.  That's because although the profile variable is automatically set, the profile itself won't be created by default.  No problem - just create the directory and the file.

Next, let's look at the command to record your session information.  You may have seen this in one of the "Tip of the day" little tidbits that come up each time you launch the Exchange Management Shell.  If not, the command you use is

[PS] C:\>Start-Transcript <filename>

This will begin recording your session information to the file that you specify.

I don't like having to do that each time, though.  Let's make this more automated.  By that, I mean let's modify your profile.

Editing of your profile is just done with Notepad, which is the default editor for .ps1 files.  Oh yeah, by the way, you probably noticed that a .ps1 file doesn't automatically launch powershell, like a .bat file would launch a command prompt.  This is for security reasons.  This way, if you download a .ps1 file from somewhere and it contains malicous code, you can't simply double-click on it.

OK - back to the task at hand.  Once you have your profile open, we're going to add the following lines.

CD \ *Note - there is a space between the CD and the \*
$date = get-date -UFormat %m_%d_%y
Start-Transcript c:\pslogs\$date.txt -append -noclobber

This will set the working directory to the root of the C drive (gives you more real estate to work with), defines a variable called $date, and specifies that it will get the date in the format of Month_Day_Year (i.e. 07_24_07), then tells Powershell to start the transcript each time it is launched, and it uses the variable we defined earlier to automatically create a new text file based on the current date.  Additionally, since the default behavior of start-transcript is to overwrite the previous file, we are telling it to append to an existing file (if present), and the -noclobber tells it to not overwrite the previous file.

Powershell does have some other options here.  The Start-Transcript command includes a -Path parameter that you can define, but I like the way that the above works.

Do you have any helpful Powershell tricks or tips?  I'd love to hear from you.

[edit]
After further testing, it looks like when Powershell was starting, it didn't like the $date variable for the directory name. I've edited the line you add to your profile to reflect this. What I've changed it to is to set the file name with the $date variable, which seems to work fine.