title: c# remote powershell create AD/Exchange user account and modify
body: <p>We are trying to set a user’s logon script from a remote machine in C#.  However, we get the error “The term ‘Set-ADUser’ 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.”  Do you have any thoughts on how to resolve this error?</p>
  
  <pre><code>using System;
  
  using System.Security;
  
  using System.Management.Automation;
  
  using System.Management.Automation.Runspaces;
  
  
  
  namespace PowershellAdUser
  
  {
  
      class PowershellAdUser
  
      {
  
          static void Main(string[] args)
  
          {
  
              string runasUsername = @"login";
  
              string runasPassword = "pass1234";
  
              SecureString ssRunasPassword = new SecureString();
  
              foreach (char x in runasPassword)
  
                  ssRunasPassword.AppendChar(x);
  
              PSCredential credentials =
  
                  new PSCredential(runasUsername, ssRunasPassword);
  
  
  
              var connInfo = new WSManConnectionInfo(
  
                  new Uri("http://1.2.3.4/PowerShell"),
  
                  "http://schemas.microsoft.com/powershell/Microsoft.Exchange",
  
                  credentials);
  
              connInfo.AuthenticationMechanism =
  
                  AuthenticationMechanism.Basic;
  
  
  
              var runspace = RunspaceFactory.CreateRunspace(connInfo);
  
              runspace.Open();
  
              var pipeline = runspace.CreatePipeline();
  
              var command = new Command("Set-ADUser");
  
              command.Parameters.Add("ScriptPath", "logonScript.bat");
  
              command.Parameters.Add("Identity", "test.com/Users/Test User");
  
              pipeline.Commands.Add(command);
  
              var results = pipeline.Invoke();
  
              runspace.Dispose();
  
          }
  
      }
  
  }
  </code></pre>
  
  <p>We also tried adding </p>
  
  <pre><code>var command = new Command("Import-Module activedirectory");
   pipeline.Commands.Add(command);
  </code></pre>
  
  <p>after </p>
  
  <pre><code>var pipeline = runspace.CreatePipeline();
  </code></pre>
  
  <p>This is what we get when we add it</p>
  
  <p>“The term ‘Import-Module activedirectory’ 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>
  
  <p>After all that didn't work we are trying to pass the connection information and the initial session state at the same time in order to get around the previous 'Import-Module not recognized' error.  However, it seems that the function RunspaceFactory.CreateRunspace will either take a WSManConnectionInfo object or a InitialSessionState object, but not both.  We also tried to set the initial session state after creating the runspace, but the Runspace's InitialSessionState  member appears to be private.  Is there any way to initialize a runspace with a WSManConnectionInfo object or a InitialSessionState object simultaneously?</p>
  
  <pre><code>using System;
  using System.DirectoryServices;
  using System.Security;
  using System.Management.Automation;
  using System.Management.Automation.Runspaces;
  
  namespace test
  {
      class Program
      {
          static void Main(string[] args)
          {
              var target = "servername";
              var user = "login";
              user = string.Format("{0}\\{1}", target, user);
              string shell = "http://schemas.microsoft.com/powershell/Microsoft.PowerShell";
              var targetWsMan = new Uri(string.Format("http://{0}:5985/wsman", target));
  
              var password = "pass1234";
              var ssPassword = new SecureString();
              foreach (char c in password)
              {
                  ssPassword.AppendChar(c);
              }
  
              var cred = new PSCredential(user, ssPassword);
              var connectionInfo = new WSManConnectionInfo(targetWsMan, shell, cred);
  
              InitialSessionState init_state = InitialSessionState.CreateDefault();
              init_state.ImportPSModule(new[] { "ActiveDirectory" });
              using (var runSpace = RunspaceFactory.CreateRunspace(connectionInfo))
              {
                  runSpace.InitialSessionState = init_state;
                  var p = runSpace.CreatePipeline();
                  runSpace.Open();
                  var command = new Command("Set-ADUser");
                  command.Parameters.Add("ScriptPath", "logonScript.bat");
                  command.Parameters.Add("Identity", "test.com/Users/Test760 Blah760");
                  p.Commands.Add(command);
  
                  var returnValue = p.Invoke();
                  foreach (var v in returnValue)
                      Console.WriteLine(v.ToString());
              }
  
  
              Console.WriteLine("Press any key to exit.");
              Console.ReadKey();
          }
      }
  }
  </code></pre>
  
  <p>In addition, we also experimented with using the "dsadd" command instead of the "Set-ADUser" command.  If we call "dsadd" without any parameters, it will return its help information.  However, if we try to pass any parameters, it does not throw any errors, but it does not appear to execute the command either.  Does anyone know how to call the "dsadd" command from the Pipeline object?</p>
  
  <pre><code>using (var runSpace = RunspaceFactory.CreateRunspace(connectionInfo))
  {
      runSpace.InitialSessionState = init_state;
      var p = runSpace.CreatePipeline();
      runSpace.Open();
  
      Command cmd = new Command("dsadd");
      cmd.Parameters.Add("ou", "\"OU=test5,OU=Users,DC=test,DC=com\"");
  
      var returnValue = p.Invoke();
      foreach (var v in returnValue)
          Console.WriteLine(v.ToString());
  }
  </code></pre>
  
  <p>Additional information: The term 'Set-ADUser' 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>
  
  <pre><code>        var p = runSpace.CreatePipeline();
          runSpace.Open();
          Command com1 = new Command("Import-Module");
          com1.Parameters.Add("Name", "ActiveDirectory");
          p.Commands.Add(com1);
          Command command = new Command("Set-ADUser");
          command.Parameters.Add("Identity", "tuser19");
          command.Parameters.Add("ScriptPath", "logonScript.bat");
          p.Commands.Add(command);
          var returnValue = p.Invoke();
  </code></pre>