title: C# run interactive programs such as cmd.exe inside a powershell runspace
body: <p>I want to run an interactive program such as cmd.exe inside a powershell runspace.
  I tried to call it but I get the output of the cmd.exe without being able to send the commands interactively</p>
  <p>here is the result and the code :</p>
  <pre><code>PS&gt; cmd.exe
  Microsoft Windows [Version 10.0.22000.194]
  (c) Microsoft Corporation. All rights reserved.
   
  C:\Users\User\Documents&gt;
  PS&gt;
  </code></pre>
  <pre><code>using (PowerShellProcessInstance pspi = new PowerShellProcessInstance())
  {
      string psfn = pspi.Process.StartInfo.FileName;
      psfn = psfn.ToLowerInvariant().Replace(&quot;\\syswow64\\&quot;, &quot;\\sysnative\\&quot;);
      pspi.Process.StartInfo.FileName = psfn;
      using (Runspace r = RunspaceFactory.CreateOutOfProcessRunspace(null, pspi))
      {
          r.Open();
   
              using (PowerShell ps = PowerShell.Create())
              {
              ps.Runspace = r;
              while (true)
              {
   
                  string cmd;
                  Console.Write(&quot;PS&gt; &quot;);
                  cmd = Console.ReadLine();
                  ps.AddScript(cmd);
                  foreach (PSObject pso in ps.Invoke())
                  {
                      Console.WriteLine(pso);
   
                  }
   
              }    
          }
   
      }
  }
  </code></pre>