PowerShell + WPF + XAML = Cool

To create a GUI for your script try using WPF + XAML.

Here is a few things that worked for me.

Lets start with a basic script.

 1: Add-Type -AssemblyName presentationframework
 2:
 3: [xml]$XAML = @"
 4: <Window
 5:   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 6:   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 7:   Height="150" Width="150">
 8:
 9: </Window>
 10: "@
 11:
 12: $Reader=(New-Object System.Xml.XmlNodeReader $xaml)
 13: $Window=[Windows.Markup.XamlReader]::Load( $Reader )
 14:
 15:
 16: $Window.ShowDialog() | out-null

That should make a small blank window appear.

image

So what you may be thinking! what good is that?

But be patient please this is only the beginning.

Lets make some text appear in the window.

 1: Add-Type -AssemblyName presentationframework
 2:
 3: [xml]$XAML = @"
 4: <Window
 5:   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 6:   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 7:   Height="150" Width="150">
 8:
 9:     <Label Content="Hello World!"/>
 10:
 11: </Window>
 12: "@
 13:
 14: $Reader=(New-Object System.Xml.XmlNodeReader $xaml)
 15: $Window=[Windows.Markup.XamlReader]::Load( $Reader )
 16:
 17:
 18: $Window.ShowDialog() | out-null

So guess what might happen?

image

So have I got your attention yet?

Ok it’s still not that exciting but hopefully you are following along.

To make it a tiny bit better to look at lets center the label in the window.and add a bit of colour.

 1: Add-Type -AssemblyName presentationframework
 2:
 3: [xml]$XAML = @"
 4: <Window
 5:   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 6:   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 7:   Height="150" Width="150"
 8:   Background="lightblue">
 9:
 10:    <Label HorizontalAlignment="Center"
 11:           VerticalAlignment="Center"
 12:           FontSize="18"
 13:           Foreground="White"
 14:           Content="Hello World!"/>
 15:
 16: </Window>
 17: "@
 18:
 19: $reader=(New-Object System.Xml.XmlNodeReader $xaml)
 20: $Window=[Windows.Markup.XamlReader]::Load( $reader )
 21:
 22: $Window.ShowDialog() | out-null

image

Now that we have it looking a bit better lets change the content of our label via PowerShell.

To be able to change the content of the label we have to do three things:

Give the label a name, find the label in the xaml and then set the content to the new value.

 1: Add-Type -AssemblyName presentationframework
 2:
 3: [xml]$XAML = @"
 4: <Window
 5:   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 6:   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 7:   Height="150" Width="150"
 8:   Background="lightblue">
 9:
 10:    <Label HorizontalAlignment="Center"
 11:           VerticalAlignment="Center"
 12:           FontSize="18"
 13:           Foreground="White"
 14:           Content="Hello World!"
 15:           Name="myHostName" />
 16:
 17: </Window>
 18: "@
 19:
 20: $reader=(New-Object System.Xml.XmlNodeReader $xaml)
 21: $Window=[Windows.Markup.XamlReader]::Load( $reader )
 22:
 23: $myHostName = $Window.findname("myHostName")
 24:
 25: $myHostName.Content = $env:computername
 26:
 27: $Window.ShowDialog() | out-null

image

The change to the script will display you computer name in the window.

 

This has been a very basic intro but it should be enough to get you going.

Advertisement
Tagged , , ,
Follow

Get every new post delivered to your Inbox.