title: Running PowerShell commands from C#
body: <p>I have powershell script which connects to exchange online and performs tasks like creating shared mailboxes, calendars, adding licenses etc.</p>
  
  <p>The thing is that when I run those commands from C# class I get the error.</p>
  
  <p>This is my connection:</p>
  
  <pre><code> string schemaURI = "http://schemas.microsoft.com/powershell/Microsoft.Exchange";
          Uri connectTo = new Uri("https://outlook.office365.com/powershell-liveid/");
  
          var securePassword = new SecureString();
  
          foreach (char c in password)
          {
              securePassword.AppendChar(c);
          }
  
          this.credential = new PSCredential(login, securePassword);
          WSManConnectionInfo connectionInfo = new WSManConnectionInfo(connectTo, schemaURI, credential);
          connectionInfo.MaximumConnectionRedirectionCount = 5;
          connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
  
  
              this.remoteRunspace = RunspaceFactory.CreateRunspace(connectionInfo);
              this.remoteRunspace.Open();
  </code></pre>
  
  <p>This is my PowerShell which is working fine:</p>
  
  <pre><code>  PowerShell powershell = PowerShell.Create();
                  powershell.Runspace = this.remoteRunspace;
                  PSCommand command = new PSCommand();
  
                  command.AddCommand("new-mailbox");
                  command.AddParameter("Name", mailboxName);
                  command.AddParameter("Shared");
                  command.AddParameter("PrimarySmtpAddress", formatedMailboxName);
                  powershell.Commands = command;
  
                  powershell.Invoke();
  </code></pre>
  
  <p>And this is the code which is not working fine:</p>
  
  <pre><code> PowerShell powershell = PowerShell.Create();
                  powershell.Runspace = this.remoteRunspace;
                  PSCommand command = new PSCommand();
                  command.AddCommand("Connect-MsolService");
                  command.AddCommand("Import-Module MSOnline");
                  command.AddParameter("Credential", this.credential);
                  command.AddCommand("Set-MsolUser");
                  command.AddParameter("UserPrincipalName", userLogin);
                  command.AddParameter("UsageLocation", "SE");
  </code></pre>
  
  <p>The error I got is following:</p>
  
  <blockquote>
    <p>Additional information: The term 'Import-Module MSOnline' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.</p>
  </blockquote>
  
  <p>I tried various things like copy-paste dll's and changing active solution platform CPU to 64 bit and none of it helped me.</p>