How to Host PowerShell in a WPF Application

by Doug Finke on September 2, 2009

in Hosting,How To,IronPython,IronRuby,LUA,PowerShell,WPF

Colleague Mike Davey was talking about hosting a scripting engine in his WPF application. He is at a choice point and is considering LUA, PowerShell, IronPython or IronRuby.

One question he has is how to pass part of your object model into PowerShell so you can manipulate it with script.

This short screen cast shows how to host the PowerShell scripting engine, create a variable and set it to a WPF visual control. You can use this approach to set variables to instances of your object model as well.

{ 7 trackbacks }

Twitted by manudurand
09.03.09 at 8:33 am
Tweets that mention How to Host PowerShell in a WPF Application --
09.03.09 at 9:17 pm
Writing your own PowerShell Hosting App (Part 1. Introduction) « PowerShell Station
10.12.09 at 9:12 pm
Writing a PowerShell script: What’s the right tool for the job? « brain | get-content
01.10.10 at 4:05 pm
NYC Code Camp PowerShell Talk
02.21.11 at 6:37 pm
SharePoint Dev Lab » The pain & gain of SharePoint solution deployment process, part 1
03.16.13 at 8:15 pm
SharePoint Development Lab by @avishnyakov » The pain & gain of SharePoint solution deployment process, part 3 – Appropriate deployment process
04.02.13 at 5:02 am

{ 8 comments… read them below or add one }

Mike 09.03.09 at 8:17 am

Thanks, Doug. This was a lot of help.

Jason 09.03.09 at 12:34 pm

Nice demo.

While your example works fine, the InitialSessionState api was designed for this scenario. It will create the variables as part of the runspace creation rather than using SessionStateProxy to add variables after the runspace is opened.

Doug Finke 09.03.09 at 7:49 pm

Thanks Jason, great info I will re-work the code.

Mike Shepard 09.03.09 at 9:39 pm

Nice demo. Probably about as short as possible, and still allows you to see the power in the approach. I would definitely vote for powershell as a scripting language. I have built a custom host that has some custom cmdlets to interact with the environment, and it makes customization really easy. I’m using 1.0 instead of 2.0 (since 2.0 isn’t final it’s hard to get support from mgmt), so I have to jump through some hoops to affect the controls, but the end result is a lot of fun.

Klaus Graefensteiner 12.28.09 at 1:06 am

Great video. Exactly what I was looking for. Thanks for sharing this.

RAJEEVAN 03.09.12 at 3:06 am

Nice one. Some powershell cmdlets need user input (prompts). is there any way to insert them?

Doug Finke 03.09.12 at 10:22 am

Thanks for the comment Rajeevan. The answer is yes. I don’t know how to do it. The PowerShell Integrated Scripting Environment (ISE) does just that. My guess to handling that is an override when building the custom host.

I’ll look into it and will post a response when I have a solution.

Post a question to StackOverflow and tweet it.


Tarun Arora 07.19.12 at 5:56 pm

How can I import an initial module?

Runspace rs = RunspaceFactory.CreateRunspace();
rs.ThreadOptions = PSThreadOptions.UseCurrentThread;


// How can i import a default module to the runspace?
// rs.InitialPSModule = .ImportPSModule(new[] { @”C:\Intellitrace\Microsoft.VisualStudio.IntelliTrace.PowerShell.dll” });

Powershell ps = Powershell.Create();
ps.Runspace = rs;

ps.AddScript(@”Start-IntelliTraceCollection ‘DefaultAppPool’ C:\Intellitrace\collection_plan.ASP.NET.trace.xml C:\IntellitraceLogs”);


Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous post:

Next post:

You are forbidden!