Video Link - How to Embed PowerShell Within a C# Application

Join James Brundage, Tester from the Windows PowerShell team, and Bruce Kyle for a quick introduction to how embed PowerShell within your C# application.   

See how you can easily reference the PowerShell assembly and start embedding PowerShell cmdlets inside of a C# application with PowerShell V2.

 

Sample Code:

using System.Management.Automation;
using System.Management.Automation.Runspaces;


/* Calls the Get-Process PowerShell command and returns the output as a string */

foreach (string str in PowerShell.Create().
    AddScript("Get-Process").
    AddCommand("Out-String").Invoke<string>())

{

    Console.WriteLine(str);

}

NOTE: You'll need to add a reference to the version of PowerShell you have installed in your GAC to the Visual Studio project file. To do so, you'll open the project file as a text file and add the following line into the <ItemGroup> section:

<Reference Include="System.Management.Automation" /> 

 

 

John O'Donnell
Microsoft Dynamics ISV Architect Evangelist
Microsoft Corporation
 (b) http://blogs.msdn.com/jodonnell

 (b) http://blogs.msdn.com/usisvde

 (t) http://www.twitter.com/jodonnel