Entries:
Comments:
Posts:

Loading User Information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading User Information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

How to Embed PowerShell Within a C# Application

Download

Right click “Save as…”

Embed code for this video

Copy the code above to embed our video on your website/blog.

Close

Video format

Note: These selections will fall back to the next best format depending upon browser capability.

Close

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" /> 

For more news, tips, and links to developer training, see the US ISV Community blog.

Tags:

Comments Closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.