title: Run Exchange Powershell command from C#
body: <p>I am trying to run EMC commands in C#. I am running this from my personal PC that has exchange management tools installed on it. </p>
  
  <p>Our exchange servers have 2007 running on them. </p>
  
  <p>The thing is, when I run Powershell or EMC, I need to run as a different user that has exchange server 2007 permissions since my individual profile doesn't have these permissions.</p>
  
  <p>That being said, this is my code I have running on my personal PC:</p>
  
  <pre><code>RunspaceConfiguration config = RunspaceConfiguration.Create();
  PSSnapInException snapEx = null;
  
  PSSnapInInfo info = config.AddPSSnapIn("Microsoft.Exchange.Management.Powershell.Admin", out snapEx);
  
  Runspace runspace = RunspaceFactory.CreateRunspace(config);
  runspace.Open();
  
  Command createCMD = new Command("Get-Mailbox ID");
  
  Pipeline pipe = runspace.CreatePipeline();
  pipe.Commands.Add(createCMD);
  
  Collection&lt;PSObject&gt; results = pipe.Invoke();
  </code></pre>
  
  <p>The error I am getting is:</p>
  
  <p>The Windows PowerShell snap-in <code>Microsoft.Exchange.Management.Powershell.Admin</code> is not installed on this computer.</p>
  
  <p>I am getting it when I try and add the <code>Microsoft.Exchange.Management.Powershell.Admin</code> snapIn.</p>
  
  <p>I feel this has something to do with my permissions on my individual profile, but I am not entirely sure. If it is true, how do I fix this.</p>
  
  <hr>
  
  <p><strong>EDIT</strong></p>
  
  <p>The reason I say it sounds like permissions is because I am able to open powershell and add the snapin. However when I run a command such as <code>get-mailboxstatistics myUserId</code> it throws an error saying <code>MyServer\MyStorageGroup</code> does not exist. However, when I shift-rightCLick and run as different user and use the credentials of my exchange admin account, I am able to run these commands.</p>