Bart Simons

Bart Simons

Thoughts, stories and ideas.

Bart Simons



.net .net 5 .net core Apache C# CentOS LAMP NET Framework Pretty URLs Windows Server WireGuard access log add analysis android api at the same time authentication authorization automate automation azure azurerm backup bash basics batch bootstrap build capture cheat sheet chromium chroot class cli click to close code coverage code snippet command line commands compile compiling compression containers control controller controlling convert cpu usage create credentials csv csvparser curl data dd deployment desktop detect devices disable diskpart dism distributed diy docker dom changes dotnet core drivers ease of access encryption example export file transfer files fix folders framework generalize getting started ghost gui guide gunicorn gzip html html tables icewarp igd imagex import inotify install installation interactive ios iphone itunes java javascript jquery json kiosk kotlin linux live load data loading screen lock screen loopback audio lxc lxd lxml macos manage manually message messages minio mirrored mod_rewrite monitor monitoring mstest mutationobserver mysql net 5 nexmo nginx no oobe node node.js nodejs not installing notification notifications object storage on desktop one command openssl owncloud parallels parallels tools parse perfect philips hue play port forwarding powershell processing ps-spotify python quick raspberry pi record rip ripping rsync rtmp save save data sbapplication scraping script scripting scriptingbridge scripts security send server service sharedpreferences sms songs sonos spotify spotify api spotlight ssh stack streaming streamlink studio sudo swarm swift sync sysprep system audio systemd tables terminal testing tracking tutorial twilio ubiquiti ubuntu ubuntu 18.04 ui code unifi unlock unsplash source upnp uptime usb tethering wallpapers wasapi website websites webview windows windows 10 without itunes without oobe workaround xaml

Create GUI windows with PowerShell and XAML

GUI windows bring modern ways of interaction to your application and that is simply great: daily used applications like web browsers and e-mail client are all part of it. PowerShell has been proven to be great for automating tasks, but don't you sometimes want to bring GUI interaction to your script?

Visual Studio has great built-in functionality to design and create XAML layouts for your application:

XAML Designer in VS

The workflow is easy: just fill your form with items from the toolbox and copy all the XML code inside the XAML text editor. Start with a new PowerShell file,

Start with a new PowerShell file, and start with pasting the XML inside an PowerShell XML object like this:

[xml]$XAMLMain = @'
        Title="Demo application" Height="350" Width="525">
        	<Label x:Name="label" Content="PowerShell + XAML demo application." HorizontalAlignment="Center" Margin="38,10,37.4,0" VerticalAlignment="Top" RenderTransformOrigin="1.056,1.635" Cursor="" FontSize="24" FontWeight="Bold"/>
        	<Border BorderBrush="Black" BorderThickness="1" Height="238" Margin="20,57,20,0" VerticalAlignment="Top"/>

The code copied from Visual Studio also contains the following:


One can strip these tags out of the copied code safely, the script won't run with this code included, so that's why this is necessary.

Next up is creating the code that launches the form. Append the following code to the end of the PowerShell file:

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

$windowMain.ShowDialog() | out-null

Run the code, and you should be presented with a form like this:


Bart Simons

Bart Simons

View Comments