≡ Menu

Create an Exchange 2003 Mailbox with PowerShell and CDOEXM

This is one of those questions that comes up every once in a while – How can I create an Exchange 2003 Mailbox using PowerShell? Let me first say that creating mailboxes using PowerShell is better suited for Exchange 2007 or 2010. That being said, if you are determined to get this done, it is possible to create Exchange 2003 mailboxes using PowerShell and CDOEXM, but the process is a little tricky.

You may be wondering…what is CDOEXM? CDO for Exchange Management (CDOEXM) is a COM component that provides classes and interfaces used to manage Exchange 2000 or 2003. CDOEXM gets loaded on a machine when you install the Exchange tools, and is the recommended component to use when programatically creating mailboxes in Exchange 2000/2003. Below you will find the steps I used to create an Exchange 2003 mailbox using PowerShell and CDOEXM:

Step 1: Create an Interop Assembly for CDOEXM

Those of you searching for examples may have seen references to interop.cdoexm.dll in code samples on various blogs and websites. In order to use CDOEXM in a PowerShell script, you first have to create an interop assembly using the COM Interop utility (TlbImp.exe). This will create a .NET wrapper for the CDOEXM component. The TblImp utility can be installed with the .Net Framework SDK or as part of Visual Studio. In this case, I am using Visual Studio 2008 and TblImp is located in %Program Files%\Microsoft Visual Studio 8\SDK\v2.0\Bin. The following syntax can be used to create the assembly:

.\TlbImp.exe “C:\Program Files\Exchsrvr\bin\cdoexm.dll” /out:c:\interop.cdoexm.dll

Step 2: Create a PowerShell Function

Next, I created a function based on the example in this post – I had to tweak the code to get it working in PowerShell v2. I also cheated and used the Quest AD cmdlets because it provides an easy way to grab the user and mailbox store DN based on the values of the parameters. Take a look at the code to see what I mean:

Function Add-2003Mailbox {
  [CmdletBinding()]
  param(
    [Parameter()]
    [System.String]
    $User,
    [Parameter()]
    [System.String]
    $MailboxStore
    )

  begin {
    $assembly = Add-Type -Path "c:\interop.CDOEXM.dll" -PassThru
    $assembly_type = $assembly | ?{$_.name -eq "IMailboxStore"}
    $binding_flags = [Reflection.BindingFlags] "invokemethod,public"
  }

  process {
    $account = [adsi]"LDAP://$(Get-QADUser $User | select -ExpandProperty DN)"
    $mailboxstoreDN = Get-QADObject -LdapFilter "(objectClass=msExchPrivateMDB)" `
    -SearchRoot (Get-QADRootDSE ).ConfigurationNamingContext |
    ?{$_.name -eq $MailboxStore} |
    select -ExpandProperty DN

    $assembly_type.invokemember("createmailbox",
              $binding_flags,
              $null,
              $account.psbase.nativeobject,
              $MailboxStoreDN)
    $account.psbase.commitchanges()
  }
}

This function will mailbox enable an existing Active Directory account using the user name and mailbox store name as parameters. Keep in mind that even though we’re importing the interop.cdoexm.dll assembly, this still needs to be run from a machine with CDOEXM installed. So, in order to use this function, you’ll need PowerShell v2, the Quest AD cmdlets and the Exchange 2003 tools installed.

Step 3: Create a Mailbox

Now that we have the function created, we are ready to add a mailbox to an existing Active Directory user. All we need to do is provide the account name and Exchange 2003 Mailbox Store name as parameters. For example, this would mailbox enable a user named testuser and create the mailbox in the “Mailbox Store (EX01)” store:

Add-2003Mailbox -User testuser -MailboxStore "Mailbox Store (EX01)"

So, that’s all there is to it. I tested all of this from a Windows Server 2003 R2 SP2 box running Exchange 2003 SP2 and PowerShell v2 – there were some quirks to work through along the way. Again, if you are looking to automate Exchange I would highly recommend upgrading to 2007/2010.

6 comments… add one

  • LuchtSiuil May 3, 2010, 8:21 am

    RT @mike_pfeiffer: [Blog] Create an #Exchange 2003 Mailbox with #PowerShell and CDOEXM http://www.mikepfeiffer.net/2010/05/crea

  • LuchtSiuil May 3, 2010, 8:21 am

    RT @mike_pfeiffer: [Blog] Create an #Exchange 2003 Mailbox with #PowerShell and CDOEXM http://www.mikepfeiffer.net/2010/05/crea

  • Paul Bendall May 3, 2010, 10:42 am

    Great example of how PowerShell is so flexible. Thanks also for mentioning TlbImp.exe as I wasn’t aware of this utility. I’ve referenced your write-up on my Blog http://paulbendall.spaces.live.com/blog/cns!D0153463C8633A7C!389.entry

  • Mike Pfeiffer May 3, 2010, 10:59 am

    Thanks Paul.

  • Lukasz April 11, 2012, 3:34 am

    It seems fine but it doesn’t work for me. I get following error:
    Exception calling “InvokeMember” with “5″ argument(s): “Number of parameters specified does not match the expected number.”
    At line:9 char:28
    + $assembly_type.invokemember <<<< ("createmailbox",
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodTargetInvocation
    Event when I attempt use this way directly on exchange server

  • SalvaG May 31, 2013, 12:23 am

    Hi
    Mike/ Lukasz : the error posted by Lukasz is shown if you use x64 version of powershell. Exchange 2003 DLLs only works using x86 version. If you add this piece of code you can run it directly from x64

    if ($env:Processor_Architecture -ne “x86″)
    {
    write-warning ‘Launching x86 PowerShell’
    & “$env:windir\syswow64\windowspowershell\v1.0\powershell.exe” -noninteractive -noprofile -file $myinvocation.Mycommand.path -executionpolicy bypass
    exit
    }

    Thanks

    Salva

Leave a Comment