pauls page

for music, code and the internet of things.

Windowless Apps with PowerShell

What?! You can Create and Deploy completely custom apps with a single Powershell script? Turns out it’s quite easy.

If you want the jump on the explanation below, grab my demo script from here – else read on to find out more.

The WYSIWYG way to layout a custom GUI is to use Microsoft’s XAML Studio from the Windows app store, but in this post we’re doing it the hacker way by pulling apart a working example – how fun!

First Things

Grab the script and open it in PowerShell ISE. You can run it straight away and see what it looks like. If you get a security error, you likely need to allow script execution.

Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope CurrentUser

The User Interface

Now lets break down what’s happening in this script.

First we load up the .NET Windows Presentation Foundation framework, and then we begin a string variable $xaml which contains our XAML code for the GUI.

The XAML is approximately structured as such…

<Window>
  <Grid>
    <Grid Definitions/>
    <Images/>
    <Buttons/>
    <TextBoxs/>
  </Grid>
</Window>

Change this value to True to see the Grid layout…

The Row and Column definitions should be relatively self-explanatory. The various Height and Width values are calculated as such…

First, Auto = Fit to the content
Then *, 2*, 3* = “*” is what is left over after all the Autos are calculated and is split up in ratios e.g. A single * will be half the size of 2*.

Then we have all our images and buttons etc. I’ll leave you play with- and discover all that later on.

The Code

A bit further down and we have the meat and potatoes. We pull in that big $xaml string and load it into a $window object.

Then we make objects for all the things, via their x:Name attributes.

Add some Event handlers…

and then we stream in the bitmap data for the background and logo images.

The $bubble_blue and $shitlogo vars are 24bit PNG files converted to Base64 string. Here’s a great online converter.

This is how we get all that graphics into a flat-file script – neat huh?

Finally the $window.ShowDialog() | Out-Null … this fires up the GUI and the pipe to Out-Null traps any errors when you quit.

Really, that’s it.

Launch the script from command line, or via shortcut etc. You can even wrap it in an executable with PS2EXE.

powershell .\MyXAMLapp.ps1 -WindowStyle Hidden

Enjoy.