How do I programatically create an exchange 2010 mailbox using C#


I have been given a task to write a program to automatically create a 2010 exchange mailbox. My research tells me to use powershell but I can't seem to find the namespace to reference and would like some sample code. I found some code on the web but I don't know what the namespace is for PowerShell. I think it might be System.Management.Automation but when I try to reference the namespace it does not exist in the list of dotnet. All I have is System.Management and System.Management.Instrumentation.

Any help would be appreciated?


When I did it I had to download Powershell separately, not sure if this is still the case though. You can get it from here.

Here is example code that will create a Mailbox:

SecureString password = new SecureString();
string str_password = "pass";
string username = "userr";

string liveIdconnectionUri = "";

foreach (char x in str_password)

PSCredential credential = new PSCredential(username, password);

// Set the connection Info
WSManConnectionInfo connectionInfo = new WSManConnectionInfo((new Uri(liveIdconnectionUri)), "",

connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Default;

// create a runspace on a remote path
// the returned instance must be of type RemoteRunspace

Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);

PowerShell powershell = PowerShell.Create();
PSCommand command = new PSCommand();

command.AddParameter("Identity", usercommonname);
command.AddParameter("Alias", userlogonname);
command.AddParameter("Database", "MBX_SBG_01");

powershell.Commands = command;
    // open the remote runspace
    // associate the runspace with powershell
    powershell.Runspace = runspace;
    // invoke the powershell to obtain the results
    return = powershell.Invoke();
catch (Exception ex)

    // dispose the runspace and enable garbage collection
    runspace = null;
    // Finally dispose the powershell and set all variables to null to free
    // up any resources.
    powershell = null;