So in a post that has NOTHING to do with anything else I’ve posted on here, I thought I’d share a piece of PowerShell/Exchange scripting I ended up writing for work.  I’ll update this post as I make changes, but here’s the code.  Feel free to use or modify as needed.  Please note this was written in PowerShell 1, and if you destroy your exchange implementation fiddling around with this, I take 0 responsibility.

Step 1:

Make a batch file.  This file will be used so that you can easily execute your script, or use Windows Task Scheduling to automate when this script is run.  To do this just open a text editor and save a file with the .bat extension.  You will want one line inside of the file as follows

PowerShell.exe -PSConsoleFile “C:\Program Files\Microsoft\Exchange Server\Bin\ExShell.psc1” -Command “c:\mv2.ps1”

Please note that I’m using mv2.ps1 as my actual PowerShell file, you should change this path to point to wherever you’re saving the powershell file itself.

2) Make the powershell file!  You can just do another notepad file and save it with whatever name you use in the batch file.  Then paste the contents as below, with whatever appropriate edits

#The AddBreaks Function will format each user’s emails and present them in readable/HTML format
function AddBreak($MessageLog){
$MessageBody = “<table cellpadding=’4′><tr><td><b>Timestamp</b></td><td width=500><b>MessageSubject</b></td><td><b>Recipients</b></td></tr>”

forEach($rowItem in $MessageLog){
$MessageBody = $MessageBody + “<tr>”
$MessageBody = $MessageBody + “<td>” + $rowItem.Timestamp + “</td>”
$MessageBody = $MessageBody + “<td>” + $rowItem.MessageSubject + “</td>”
$MessageBody = $MessageBody + “<td>” + $rowItem.Recipients + “</td>”
$MessageBody = $MessageBody + “</tr>”
}
$MessageBody = $MessageBody + “</table>”
$MessageBody | out-file c:\temp.txt -append
$MessageBody = “”
}

#The Start and End variables set the period of this report, currently this displays the previous day
$MessageBody = “”
$Start = (Get-Date -Hour 00 -Minute 00 -Second 00).AddDays(-1)
$End = (Get-Date -Hour 23 -Minute 59 -Second 59).AddDays(-1)

$sender = “youremailaddress@email.com”
$recipient = “recipientemailaddress@email.com”
$CC = “ccifneeded@email.com”
$BCC = “bccifneeded@email.com”
$mailserver = “localhost”
$subject = “Email Report for $Start”
#This above block sets the email header and should be changed as required.

“<h1>Email Usage Report</h1>Please send all problems or new user requests to youremailaddress@email.com.  <br><br>” | out-file c:\temp.txt

#Complete one of these blocks for each individual you wish to have included in the report
“<br>User One<b>Sent</b> Messages: <br>” |  out-file c:\temp.txt -append
$User1Sent = get-messagetrackinglog -Sender “user.one@email.com” -EventID “SEND” -Start $Start -End $End | select Timestamp, MessageSubject,@{n=”Recipients”;e={[string]::join(“,”,$_.recipients)}}
AddBreak($User1Sent)

“<br>User One <b>Received</b> Messages: <br>” |  out-file c:\temp.txt -append
$User1Received = get-messagetrackinglog -Sender “user.one@email.com” -EventID “RECEIVE” -Start $Start -End $End | select Timestamp, MessageSubject,@{n=”Recipients”;e={[string]::join(“,”,$_.recipients)}}
AddBreak($User1Received)
#Be sure to increment the user counter and use new variables for each user.

“<br>User Two<b>Sent</b> Messages: <br>” |  out-file c:\temp.txt -append
$User2Sent = get-messagetrackinglog -Sender “user.two@email.com” -EventID “SEND” -Start $Start -End $End | select Timestamp, MessageSubject,@{n=”Recipients”;e={[string]::join(“,”,$_.recipients)}}
AddBreak($User2Sent)

“<br>User Two <b>Received</b> Messages: <br>” |  out-file c:\temp.txt -append
$User2Received = get-messagetrackinglog -Sender “user.two@email.com” -EventID “RECEIVE” -Start $Start -End $End | select Timestamp, MessageSubject,@{n=”Recipients”;e={[string]::join(“,”,$_.recipients)}}
AddBreak($User2Received)
#Here’s an example block of a second user’s email being added.  Add as many of these blocks as you need

$body = get-content c:\temp.txt
$msg = new-object System.Net.Mail.MailMessage $sender, $recipient, $subject, $body
#Uncomment the line below if you want to use attachments
#$attachment = new-object System.Net.Mail.Attachment c:\temp5.csv
$msg.CC.Add($CC)
#$msg.BCC.Add($BCC)
#Uncomment the line below if you wish to use attachments
#$msg.Attachments.Add($attachment)
$msg.IsBodyHTML = $true
$client = new-object System.Net.Mail.SmtpClient $mailserver
$client.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
$client.Send($msg)

3) The above script uses a text file to parse all this information, just  create an empty text file in a location you have write access to.  The above script uses c:\temp.txt

4) You can just run the batch file to test that everything’s working, or use Windows Task Scheduler to set this up to run on a daily basis.

5) Leave a comment here if this helps!  I hacked together this code from a ton of places, with a ton of people’s help.  I hope if you have success or improvements you’ll let me know and hopefully this helps make some people’s life easier.