Creating folders in Office 365 mailboxes using EWS

I ran into an issue this week where a client needed to programmatically create folders in shared mailboxes.  The client is a contracting firm and needs a way to collect all of the messages related to a project into a single place so that they can be easily referenced and later archived with all the other data related to the project.  Up until this point we had been creating a shared mailbox for each project with an associated transport rule that would BCC all mail that contained the project number (in a variety of formats) in the subject line.

This worked great up until the firm started growing and we began to bump into the limit of 100 transport rules in Office 365. After some discussion we decided to move to a smaller number of shared mailboxes that are assigned to each group within the company and then have the individual projects as folders within those mailboxes. This way each transport rule can contain all of the strings for each project in a mailbox and then we can use inbox rules to sort the mail once it is delivered to the mailbox. This solution was great until I started thinking about having to manage all of those folder and rules, no way was I going to do that. Powershell to the rescue!

Well I quickly found out that in Office 365 even though there is a cmdlet to allow you to create a mailbox folder new-mailboxfolder it does not and will not work on a mailbox besides the one your are logged in as, even if you have FullAccess permissions. After some digging I found a method to do just that with EWS. After a lot of google, a lot of furious typing, and 3 Jaco albums I finally was able to get everything working. I did get lucky in that for whatever reason Office 365 does allow you to use the new-inboxrule cmdlet on other mailboxes besides your own.

The following script was meant to be used by an authorized user at my client so it is pretty basic. The script will ask you a series of questions and then generate a new shared mailbox, assign FullAccess permissions to the correct users, create transport rules, create folders in the new mailbox and create inbox rules. When it’s all done it will send you an email telling you if it succeeded or not. There is also some strict assumptions about the format of the project name. The client uses a standard format so I haven’t had to make this more portable but you should be able to customize this pretty easily to your needs using regular expressions and string manipulation.

There are a few prerequisites to running these scripts. Powershellv3 or better and the EWS managed API 2.2. The script assumes the EWS DLLs are installed to The script assumes that the API is installed to C:Program FilesMicrosoftExchangeWeb Services2.2 which is the default.

I also wrote a second script that can be used to create new folders in existing mailboxes.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s