Run a PowerShell process in a C# app and interact with it
<p>I'm creating a C#/WPF app that needs to interact with PowerShell (basically, run commands and scripts). Creating the process and launching it is not a problem, that's pretty easy, but it becomes more difficult when the goal is to launch it without any script and make it run commands and scripts later :</p>

<ol>
<li>Launch the C# app</li>
<li>Run PowerShell process in parallel</li>
<li>[...] Do some other stuff [...]</li>
<li>Run commands on the process</li>
</ol>

<p>I tried multiple solutions.
With the <code>System.Diagnostics.Process</code> class, I am able to launch the process, let it run, but even if I redirect streams, writing to stdin just doesn't work:</p>

<pre><code>var startInfo = new ProcessStartInfo()
{
    FileName = "powershell.exe",
    Arguments = "-ExecutionPolicy Bypass -NoLogo -NoExit",
    CreateNoWindow = true,
    RedirectStandardError = true,
    RedirectStandardInput = true,
    RedirectStandardOutput = true,
    UseShellExecute = false,
    WindowStyle = ProcessWindowStyle.Hidden
};
_ps = new Process()
{
    EnableRaisingEvents = true,
    StartInfo = startInfo
};
_ps.Start();

[...]

_ps.StandardInput.WriteLine(TextBox_Input.Text); // No effect
</code></pre>

<p>Using <code>System.Management.Automation.PowerShell</code> class is not better, I can prepare the pipeline (add scripts to be executed), invoke it, but, I can't run scripts later keeping the process alive.</p>

<p>I need to launch the process as soon as possible to be able to send it commands and run them the faster I can (and avoid process launching that would cause a latency).</p>