Exchange Scripts

This page is meant to organize my frequently used scripts for Exchange Server.  Before using make sure you have a complete understanding of the script and use at your own risk.  Some are written by me and some are borrowed from many different places across the web, if a script is yours and you wish to have it removed, or credit, or a link-back let me know and I will happily oblige.

Add Exchange cmdlets to powershell session:
 Add-PSSnapin *exchange* -erroraction SilentlyContinue  

Exchange 2013 Message Tracking Log, 2013 does not have the message tracking tool that 2010 had so use this. You can also use -recipients with/without -sender.
 Get-MessageTrackingLog -ResultSize 500 -Start “11/20/2014” -End "11/22/2014" -Sender "username@domain.com" | select-object eventid,timestamp,source,messageid,sender,recipients,messagesubject,clientip,clienthostname,serverip,serverhostname,connectorid | Out-GridView  

Get-Mailbox Sizes and Limits.  For mailbox sizes you can either set a custom limit or use the database limit.  For that reason it is helpful to have a report that shows both both of these settings.  The report generates an html file with the tabulated data in c:\scripts called mailbox_sizes_[date.htm].  Then emails you the report, so change your smtp/email settings after #Fill these in.  It can be easily added to task scheduler to get a daily report.  Get the .ps1 from my github: https://github.com/nosage/freezing-octo-avenger

New-MailboxExportRequest, export mailboxes from txt to pst.  userlist.txt is a list of usernames, one on each line.
 $userlist= get-content "C:\userlist.txt"  
 foreach ($user in $userlist)
 {  
 New-MailboxExportRequest -Mailbox $user -FilePath "\\server\share"   
 }  

New-MigrationBatch, batch1.csv is a .csv that has a header EmailAddress, followed by a list of email addresses, new line for each one.  -Autostart will begin that job, but must be completed by hand.
 New-MigrationBatch -Name Batch1 -CSVData ([System.IO.File]::ReadAllBytes("C:\batch1.csv")) -Local -TargetDatabases DB01, DB02 -NotificationEmails admin@domain.com -Autostart  

Get-MigrationBatch, this will get the status of the migration batch that you just started, use it to keep an eye on things.
 Get-MigrationBatch -Identity Batch1  

Complete-MigrationBatch, once the status of the migration batch is initially synced, use this to complete the migration and finalize the mailbox on the destination database.
 Complete-MigrationBatch -Identity Batch1 -NotificationEmails admin@domain.com  

Disable-Mailbox, for old, stale mailboxes use this to "remove" them from Exchange, and keep your mailbox list clean.  Backup first by exporting to PST.  oldusers.txt is a list of usernames, one on each line.  Read more about disabling mailboxes for old users.
 $userlist= get-content "C:\oldusers.txt"  
 foreach ($user in $userlist)   
 {  
   Disable-Mailbox $user   
 }  

Get-Mailbox LastLogonDate, use this to show the last time the mailbox was accessed and by whom.
 Get-Mailbox -server Exch1 | Get-MailboxStatistics | sort lastlogontime -descending| ft Displayname,last*  

Get-DAGHealth.ps1, this is a must have if you have a multi-node Exchange mailbox environment with DAG.  It requires a free sign up to Exchange Server Pro, which if you admin Exchange you should already have.  Make sure to update the smtp server to your own in the script.
http://exchangeserverpro.com/get-daghealth-ps1-database-availability-group-health-check-script/

Keeping C drive cleaned up
The C drive on an Exchange Server filling up would be a thing of nightmares, so it is important to keep log files from growing too large.  The biggest offender for me was the IIS logs, so I made a daily task that moves these logs off of the C drive to a folder share.