Creating Exchange 2013 Mailbox in C#

Holy moly it’s been a long time since I posted.  Here’s a quick code snippet on using C# to create a mailbox to hopefully kick a string of posts off.  This creates an AD account for the user unlike my previous article on creating a mailbox in Exchange 2010.  And since I listed a gotcha in that article, I’ll leave a tip in this one after the code. 😀

var psc = new PSCommand();

psc.AddCommand("New-Mailbox");
psc.AddParameter("Name", newUser.UserName);
psc.AddParameter("UserPrincipalName", upn);
psc.AddParameter("Alias", upn);
psc.AddParameter("PrimarySmtpAddress", string.Format("{0}@{1}", newUser.UserName, domainName).ToLower());
psc.AddParameter("OrganizationalUnit", domainDn);
psc.AddParameter("SamAccountName", samAccount);
psc.AddParameter("Password", _ps.CreateSecurePassword("mysupersecretpassword"));
psc.AddParameter("DomainController", domainController);

PowerShell ps = PowerShell.Create();
ps.Commands = psc;

try
{
    var result = ps.Invoke();
    if (!ps.HadErrors)
    {
        Console.WriteLine("Congrats! User created successfully");
    }
}
catch (RemoteException ex)
{
   //Error conditions here...
}

Easy enough.  Now for the tip.  If you specify the PrimarySmtpAddress it will automatically set the EmailAddressPolicyEnabled value to false.  So leave that parameter out if it’s important for your org.

 

Tuesday, February 11th, 2014 C#, Code, Exchange

6 Comments to Creating Exchange 2013 Mailbox in C#

  • Glen Wolinsky says:

    I’m not a scripting guy, so could you show me how to do that using different credentials?

    Thanks,
    Glen

  • Steve Gaines says:

    Nice article. I’m trying to create a mailbox using this code and after a few hiccups it ran, but when I check the user’s Active Directory account the Email address is still blank. Apparently I don’t have the latest System.Management.Automation reference because I don’t see the HadErrors property. Anyway, if I can get the latest reference is there a way to check what those errors are? I’ve looked at the PSObjecs that are returned from the Invoke method and they seem to be empty. Any ideas?

    • Steve Gaines says:

      I went to nuGet and updated the reference to System.Management.Automation to version 3.0, but the program crashes now with the error

      Common Language Runtime detected an invalid program.

      Maybe the server where I run this code doesn’t have PowerShell 3.0.

      • Chris says:

        Hi Steve. When you added the package from NuGet, did you use the package for Powershell 3.0? Your other option for referencing the dll is to unload the project and to add this to the ‘< ItemGroup >‘ section.


        < Reference Include="System.Management.Automation" / >

        MSDN (https://msdn.microsoft.com/en-us/library/system.invalidprogramexception(v=vs.110).aspx) says it’s generally a bug in the compiler used. What does your setup look like for development? The Exchange server you run this against should have 3.0 installed.

        I would tell you to debug the app in VS and see where things are breaking.

        • Steve says:

          Chris

          I resolved the problem last week at work. Basically I had to make two changes. I stayed with PowerShell 2.0 because I couldn’t get 3.0 to work on my machine. I found out I should use enable-mailbox if the Active Directory account already exists. Finally I had to change DomainController to Database. Maybe that’s a 2.0 vs. 3.0 difference. Anyway, now it works and I’m on to my next task.

          Thanks

  • Leave a Reply to Steve Gaines

    Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    WordPress SEO fine-tune by Meta SEO Pack from Poradnik Webmastera