How to create Exchange Mail Users in bulk using ‘Enable-MailUser’, you idiots

First things first: Mail Users are NOT Mailbox Users. This is a fundamental mixup that inattentive morons seem to frequently fall foul of on Exchange forums. Creating Mailbox Users, being users who have a mailbox in your Exchange organisation, is so easy to do in bulk that it’s even in the Exchange Management Console GUI, so there’s no point in me or anyone else explaining how to do it.

Mail Users, on the other hand, are users that exist in Exchange but have an external address and no mailbox in your organisation. Unlike a Mail Contact, however, this user has an AD account in your forest. Creating these in bulk is a bit trickier and very badly documented. The reason probably stems from the fact that a) the Exchange developers hate anyone without an Exchange mailbox, and b) that the underlying PowerShell commands in Exchange 2007/2010 do not work the same way. And let’s face it, as much as I prefer a GUI for almost any admin task (due to not being a pretentious masochist), the Exchange Management Console is really just a front end for PowerShell.

No, a single line of PowerShell to do this is simply out of our reach. For this, we have to resort to… TWO WHOLE LINES of PowerShell:

$enableusers = Get-User -Filter {Department -Like "8*" -And RecipientType -eq "User"}
$enableusers | foreach { Enable-MailUser $_ -externalEmailAddress "$($_.name)@angrytech.com" }

This is the code I used today to automatically add mail users for all my Year 8 students (whose Department field in AD is filled in with their form, i.e. “8B”). There are plenty of other fields you can filter on. The first line retrieves the list of users, deliberately only picking User objects (not MailUser) to avoid trying to add a mail user twice. The result set of this is then enumerated by Enable-MailUser on the second line, specifying that the external email address should be made up of the AD users common name (CN) with @angrytech.com on the end, which is their (fake) email address on Google Apps.

If you already have email addresses filled in on the AD objects, and simply want to reuse this data, the second line would be as follows:

$enableusers | foreach { Enable-MailUser $_ -externalEmailAddress $_.WindowsEmailAddress.toString() }

And that’s how you get around the fact that the Exchange developers were too sodding lazy to have multiple Mail User creation in the EMC.

I still don’t like PowerShell.

Tags: ,

About The Angry Technician

The Angry Technician is an experienced IT professional in the UK education sector. Normally found in various states of annoyance on his blog. All views are those of his imaginary pet dog, Howard.

10 responses to “How to create Exchange Mail Users in bulk using ‘Enable-MailUser’, you idiots”

  1. ScottishTech says :

    Good stuff – I’ll pass this onto our IT chaps in case they ever need it.

    And I know what you mean about PowerShell – I’ve only played a little while, but I still prefer AutoIt3 :)

  2. Stephen says :

    We went for Google Apps, and got rid of an awful lot of headaches and storage pains. I’m sure there are other tangible costs and other issues, but they haven’t reared their heads yet.

  3. JD says :

    You can certainly do it on one line no need to create a variable just pipe it directly ..

    Get-User -Filter {Department -Like “8*” -And RecipientType -eq “User”} | % { Enable-MailUser $_ -externalEmailAddress “$($_.name)@angrytech.com”}

    • AngryTechnician says :

      Aha! I didn’t know about the % alias (aka ForEach-Object). I will definitely give that a go, thanks.

    • AngryTechnician says :

      OK, so got around to I trying this today when provisioning some new users, and it mostly worked, but I got a lot of errors stating “Pipeline not executed because a pipeline is already executing. Pipelines cannot be executed concurrently”, which is exactly the same error I was getting before.

  4. Jay says :

    Hi,

    Instead of using ‘Department’ as an arguement, how would we be able to filter from a specific OU and whether the User Objects are Enabled?

    Thanks

    • AngryTechnician says :

      Run Help Get-User -full and look for the section on using the -OrganizationalUnit parameter for the OU part. I don’t know how to exclude disabled users though; I always just move mine to a separate OU.

  5. Jon says :

    How do you add the external to be firstname.lastname

Trackbacks / Pingbacks

  1. Live@Edu - Routing Issues & Shared Address Space - 9th March, 2012