Enable Exchange Mailboxes for All Users in an OU

This is more of a note to myself than anything, but what the heck, why not share it with everyone else?

I frequently build new SharePoint farms for development and testing, and I usually create a new domain controller and populate it with users.  One of my favorite scripts is by Mark Rhodes, “Adding 285 Contoso Users with Pictures to your Development Environment Active Directory”.  The result is just as the title indicates:

image

Much less frequently I set up Exchange in the environment as well.  Usually I add the users with Mark’s script first, and then at some point later I install Exchange.  The problem is that now I have 285 users in the Employees OU that I want to have mailboxes, but I certainly don’t want to create them by hand.  Further, I don’t want the users in the Users container to have mailboxes.  The result?  A little PowerShell.

Open the Exchange Management Shell, which opens an instance of PowerShell.  Next, use the following PowerShell (substituting, of course, your searchBase parameter.

 

 Import-module activedirectory

$users = Get-ADUser -LDAPfilter '(name=*)' -searchBase {OU=Employees,DC=contoso,DC=lab} 
foreach($user in $users)
{
   Enable-Mailbox -Identity $user.SamAccountName
}
  

That script will iterate through the users, filtered by the OU that you want using the searchBase parameter.  I am sure there is room for improvement here, but it works in my lab!

image