PowerShell does Xaml

by Doug Finke on May 6, 2008

in PowerShell,WPF,XAML

Jaykul has a nice post on using WPF in PowerShell version 2. Remove xml+site:msdn2.microsoft.com”> from two places in the sample. Looks like his site generates a link to the acronym Xml.

You can inline Xaml with a here string, cast it to an Xml Document and load it with a Xaml Reader. This example shows the new Add-Type cmdlet which does a using on the WPF PresentationFramework assembly.


Start the shell using a single-threaded apartment

You must start PowerShell with the new parameter -STA

#requires -version 2

Add-Type -AssemblyName PresentationFramework

[xml] $xaml = @"
    Title="Window1" Height="300" Width="408">
        <Button x:Name='button1'
          Content='Click Here' />

$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$target=[Windows.Markup.XamlReader]::Load( $reader )

$target.ShowDialog() | out-null

{ 1 trackback }

PowerShell WPF from XML Findname Problem | Hardware Infomation and Computer Tutorials
06.29.11 at 6:04 pm

{ 2 comments… read them below or add one }

Niels Grove-Rasmussen 01.15.09 at 3:23 pm

Hi Doug,

Very nice and simple (+) example.

But – How do you equal simple implement functionality on the button?

With Respect

Doug Finke 01.15.09 at 10:58 pm

Here is one way.
Also checkout
And James Brundages’ posts on WPF
Add these lines before the ShowDialog()


$eventMethod.Invoke({$window.Title=”Hello $((Get-Date).ToString(‘G’))”})

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!