This is a well-known thing, which is implemented in the class
System.Management.Automation.PowerShell
, please see:
http://msdn.microsoft.com/en-us/library/system.management.automation.powershell%28v=vs.85%29.aspx[
^].
You can find a simple code sample here:
http://www.dougfinke.com/blog/index.php/2010/04/08/how-to-embed-powershell-within-a-c-application/[
^].
Now, this sample uses a default console as it comes in
console applications. You can use console in your Forms applications; you can show no output; or you can show output in some
System.Windows.Forms
control. I would recommend using
ListBox
to have the look closest to the traditional console. You can present the work of the shell in any other way.
If the execution of the shell can take some noticeable time, you should run it all in a separate thread. If you need to present some data produced by this PowerShell thread, you will need to use UI thread invocation, which I described in detail in my past solutions:
Control.Invoke() vs. Control.BeginInvoke()[
^],
Problem with Treeview Scanner And MD5[
^].
See also more references to my past answers on threading:
How to get a keydown event to operate on a different thread in vb.net[
^],
Control events not firing after enable disable + multithreading[
^].
—SA