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.

{ 4 trackbacks }

Twitted by manudurand
09.03.09 at 8:33 am
Tweets that mention How to Host PowerShell in a WPF Application -- Topsy.com
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

{ 5 comments… read them below or add one }

1 Mike 09.03.09 at 8:17 am

Thanks, Doug. This was a lot of help.

2 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.

3 Doug Finke 09.03.09 at 7:49 pm

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

4 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.

5 Klaus Graefensteiner 12.28.09 at 1:06 am

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

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>