Laeeq Qazi's Exchange Server Blog

Exchange Server blog

Create Mailbox on Exchange Server 2013 using C#


Creating a mailbox on Exchange 2013 takes same source code as I used on Exchange 2010.

RunspaceConfiguration runspaceConf = RunspaceConfiguration.Create();

PSSnapInException PSException = null;

PSSnapInInfo info = runspaceConf.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out PSException);

Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConf);


Pipeline pipeline = runspace.CreatePipeline();

//StringToSecureString is a function defined at the end

SecureString securePwd = StringToSecureString("Passw0rd1");
Command newMailBox = new Command("New-Mailbox");

newMailBox.Parameters.Add("Name", "TestName1");
newMailBox.Parameters.Add("Alias", "TestName1");
newMailBox.Parameters.Add("database", "Mailbox Database 1406738839");
newMailBox.Parameters.Add("Password", securePwd);
newMailBox.Parameters.Add("DisplayName", "TestName1");
newMailBox.Parameters.Add("UserPrincipalName", "TestName1@ad.lab");
newMailBox.Parameters.Add("OrganizationalUnit", "ou=myorg,dc=ad,dc=lab");
newMailBox.Parameters.Add("FirstName", "TestName1");


Collection<PSObject> result = pipeline.Invoke();

Here is the helper function

public static SecureString StringToSecureString(string str)
   SecureString ss = new SecureString();
   char[] passwordChars = str.ToCharArray();

   foreach (char c in passwordChars)
   return ss;

Note: I used VS 2010 for it. If you use any of the Exchange DLLs, like Microsoft.Exchange.Management.dll etc, then you would have to use VS 2010, as these Exchange DLLs have been developed using .Net 4.0, and you cannot add a reference of .net 4 DLL in VS 2008 or lower version of VS.

Very Important: After a healthy discussion on Exchange 2013 Forum on support of Local PowerShell sessions on Exchange 2010\2013, I don’t recommend to use above mentioned approach to create mailboxes on Exchange Server 2013, instead use Remote PowerShell sessions. I would put a post on it in coming days, where I would show how to create a mailbox on Exchange 2010\2013 using Remote PowerShell. Please visit the link mentioned in the first comment below for more details. I would also mention some problems while adopting Remote PowerShell approach like Object Deserialization issue.


Laeeq Qazi

October 2, 2012 Posted by | Exchange 2013 | , , , | 9 Comments