Clean Up Orphaned Calendar Items in Exchange 2007

Updated 3/29/2012

A common problem I’ve read about, and personally experienced, is deleting a user and their mailbox, only to find out later that they had a recurring calendar meeting in a conference room, or they were an administrative assistant, or something like that. This will cause orphaned calendar items that can be a pain to clean up. When I recently ran into this problem, I noodled around trying to find an answer. I found forums on Microsoft’s site, Experts-Exchange, etc. with nothing that was really helpful. Finally, I hit up a peer of mine, Robert Durkin. Robert sent me this link of a post by Dominic Savio that got me going in the right direction.

Dominic’s post covered the basics and had the information I needed, but it still required some playing to get what I needed. So, I ended up with three commands to clean up old orphaned calendar items:

Command 1:

Export-Mailbox -Identity <user alias> -SenderKeywords “deleted_user@company.com” -IncludeFolders “\Calendar” –DeleteContent

This command will delete all calendar appointments originating from the deleted user in a single target mailbox using that deleted user’s email address. This will ensure that only calendar appointments from that user will be deleted since we’re A) using a unique string to identify the appointments and B) specifying the Calendar folder. Be careful if you’ve added the departed user’s email as an alias to another account because I didn’t test that and I am not sure what those results would be.

Command 2:

Get-Mailbox | Export-Mailbox -SenderKeywords “deleted_user@company.com” -IncludeFolders “\Calendar” –DeleteContent

This command will delete calendar appointments originating from the deleted user in every mailbox, in the unlikely event they were meeting happy.

Command 3:

Get-Mailbox -Filter {CustomAttribute14 -eq ‘ResourceMB’} | Export-Mailbox -SenderKeywords “deleted_user@company.com” -IncludeFolders “\Calendar” –DeleteContent

I borrowed the filter portion of my last post in this command to delete the appointments originating from the deleted user in every mailbox whose Custom Attribute 14 is set to ResourceMB. I went ahead and entered this custom attribute for every conference room, projector, and video cart we have so that I can clean them all up with one command.

You can also use the –TargetMailbox parameter to redirect items to a separate mailbox instead of delete them in the event of a disaster. The full list of parameters for TargetMailbox is located here.

Quick note.  I ran into a scenario where the user’s account was already deleted, so when I would run the command, it didn’t do any clean up.  When I checked the appointment, I saw that the user had ‘No e-mail address exists for this person’ in the properties in Outlook.  Since this was the case, the command using the email address obviously didn’t work.  I replaced the email address with the displayed user name in the appointment and it worked like a champ.  The modified command looked something like this:

Get-Mailbox -Filter {CustomAttribute14 -eq ‘ResourceMB’} | Export-Mailbox -SenderKeywords “Lastname, Firstname” -IncludeFolders “\Calendar” –DeleteContent

Be sure to get the ‘Lastname, Firstname” value from what is displayed in the orphaned appointment.

Select and Clean Out Exchange 2007 Mailbox using a Custom Attribute

We were getting slim on storage on our Exchange server so I had to knock out some cleanup today. I saw that there were several service accounts with 40K messages or so apiece in them. Not sure how common these mailboxes are, but some are dumping grounds for NDRs, network/service alerts, or in our case, mail flow monitoring accounts. So, I composed this simple command to clean out the inbox of those service accounts. This script is for 2007, not 100% how different the script would be in 2010 since I know they made some significant changes to the 2010 Export-Mailbox command, but this worked like a dream in 2007:

Export-Mailbox -Identity ‘service-mailbox’ –DeleteContent

This quick script will delete all messages from the mailbox with no backup. Of course, you have to replace ‘service-mailbox’ with the alias of your service mailbox.

Now, I took it one step further. In order to repeat this process on a semi-regular basis, I added an ‘svc’ value to Custom Attribute 15. I then wrote this command to select these accounts and clean them out:

Get-mailbox -Filter { CustomAttribute15 -eq ‘svc’} | Export-Mailbox –DeleteContent

I personally ran the above command with a –WhatIf at the end of it to make sure I was going to be cleaning out the mailboxes I had intended to clean out.

I now have a command I can schedule to run monthly to keep these mailboxes under control.

Runas Batch File To Launch MMC as Domain Admin

So, I recently started with a new company that wisely implements User Account Control on the desktops, and follows the best practice of having one administration account for Domain Admins and a separate account for everyday use. I came from an environment where I was supporting several different domains so to build an MMC for my clients didn’t make much sense. Had I written this little batch file before, it would have made my onsite visits much easier. I am running this on Windows 7 SP1 x64. The batch file is quite simple, but I didn’t find anything with instructions on how to ‘make it all work’ so I thought I’d throw this blog out there.

Step 1: Check your services, and make sure the “Secondary Login” service is running and set to Automatic. If this service isn’t running, you can’t log in using runas.

Step 2: Create a folder on the C drive and name it MMC. If you are going to be visiting multiple clients, or if you are going to be administering multiple disjointed domains, add the “Everyone Full Control” permission to this folder. I’m not 100% sure if is important (and I honestly didn’t test it), but I would believe it would simplify things so that the batch file doesn’t have to get past the local permissions of your PC to also run on the domain your PC is a guest on. If you are running this on your own domain, or a domain your PC is a member of, then this isn’t as important.

Step 3: Create a new MMC with all of the Snap-Ins you like to use regularly and save it to the C:\ MMC folder. I added ADUAC, DNS, DHCP, ADS&S, RDC, Event Viewer, Services, and Computer Management. I’m sure I’ll add more later but those are the basics that I started with.

Step 4: Create a batch file in that folder with the following text where:

  • %domain% is the domain you’re logging in to.
  • %username% is the user name you’re logging in with.
  • %filename% is the name of the MMC file you created and saved in Step 3.


runas /user:%domain%\%username% “mmc C:\MMC\%filename%.msc”

For Example, if I were logging into company.com with the user ID Matt and the MMC file I created was named Console, then the script would look like this:

 runas /user:company\Matt “mmc C:\MMC\console.msc”

I initially had problems with Windows 7 and received the error: 193: console.msc is not a valid win32 application, which is why I added the “mmc ” into the script. Not sure why Windows 7 had a problem associating MMC with msc in the batch file, but specifying mmc did the trick. I read that tip here.

Step 5: Save and close your batch file naming it something that makes sense to you.

Step 6: Copy a shortcut to your desktop (or other convenient location) and change the Icon if desired.

Step 7: Launch your batch file. Be sure to Right Click and select “Run As Administrator” if your PC has UAC turned on.

Step 8: After you launch the batch file, a cmd window will pop up and you will be prompted for a password. Enter the password for the user you defined in the batch file and you’re all set.

So, you can see the different applications of this batch file. If you’re a roaming admin, you can create a separate file for each client. In my case, we acquired a company not too long ago, so I will have one batch file (and likely a different msc defaulting to certain servers) for each of the domains for ease of administration.

Still working on the Exchange Management console.  The same script throws the same ‘invalid Win32 Application’ with our without the preceeding MMC part of the script.  I can Shift-Right Click, and ‘Run as different user’, but I’d rather not…..

Good luck, and happy administration!