Exchange 2010 - Check if Mailbox Exists using C# and Remote PowerShell

The following is an example on how to use C# to check if exchange mailboxes exist in Exchange 2010 using remote PowerShell.

Imports, not all are used in the example function below:

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.DirectoryServices;  
using System.EnterpriseServices;  
using System.Configuration;  
using System.Security;  
using System.Management.Automation;  
using System.Management.Automation.Runspaces;  
using System.Collections.ObjectModel;  
using ActiveDs;  
using System.Net;  
using System.Security.Principal;  
using System.Text.RegularExpressions;  
using System.Security.Cryptography;  
using System.Security.AccessControl;  
/// <summary>
// Returns true or false if a mailbox is found to be associated with the given system (domain) account.
// </summary>
/// <param name="system">The domain of the account</param>
/// <param name="userid">The account name (samAccountName)</param>
/// <param name="exchangeRemoteServer">The URL for exchange powershell: https://exchange.subdomain.domain.org/powershell</param>
/// <returns></returns>
public bool GetMailboxExists(string system, string userid, string exchangeRemoteServer)  
{
 bool mailboxExists = false;
 try
 {
  // Assumes the process will run with proper priviledges to create mailboxes through powershell.
  WSManConnectionInfo connectionInfo = new WSManConnectionInfo(
   new Uri(exchangeRemoteServer),
   "http://schemas.microsoft.com/powershell/Microsoft.Exchange", // This shouldn't change
   PSCredential.Empty
   );
    // Skip Certificate Verification
    connectionInfo.SkipCACheck = true;
    connectionInfo.SkipCNCheck = true;
    connectionInfo.SkipRevocationCheck = true;
    connectionInfo.AuthenticationMechanism = AuthenticationMechanism.NegotiateWithImplicitCredential;
  using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
  {
   using (PowerShell powershell = PowerShell.Create())
   {
    StringBuilder psScript = new StringBuilder("");
    psScript.AppendLine("Set-ADServerSettings -ViewEntireForest $true");
    psScript.AppendLine("[bool](Get-Mailbox -ErrorAction SilentlyContinue -Identity \"{0}\\{1}\" )");

    powershell.AddScript(string.Format(psScript.ToString(), system, userid));

    runspace.Open();
    powershell.Runspace = runspace;

    Collection<PSObject> results = powershell.Invoke();
    if (results != null)
    {
     if (results.Count > 0)
     {
      mailboxExists = (bool)results[0].BaseObject;
     }
     else
     {
      mailboxExists = false;
     }
    }
   }
  }
 }
 catch (Exception ex)
 {
  // Log your errors!
 }
 return mailboxExists;
}