title: Creating PowerShell remote session using C#
body: <p>I've been trying to create a connection with PowerShell from my .Net application using C#. After connection is done when I try to create a session, it returns empty collection.</p>
  
  <pre><code>string shellUri = "http://schemas.microsoft.com/powershell/Microsoft.PowerShell";
  
  PSCredential remoteCredential = new PSCredential("userID", StringToSecureString("Password"));
  
  WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false, "Ip Address of server", 5985, "/wsman", shellUri, remoteCredential, 1 * 60 * 1000);
  
  runspace = RunspaceFactory.CreateRunspace(connectionInfo);
  connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
  
  runspace.Open();
  
  using (PowerShell ps = PowerShell.Create())
  {
  ps.Runspace = runspace;
  
  ps.AddScript(@"$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://servername/poweshell -Credential " + remoteCredential);
  
  //result count returned is 0 
  var result = ps.Invoke();
  
  ps.Commands.Clear();
  
  ps.AddCommand("Import-PSSession $Session");
  
  ps.Invoke();
  }
  </code></pre>