Behind the PowerShell Pipeline logo

Behind the PowerShell Pipeline

Subscribe
Archives
March 14, 2024

Stacking Up with WPF

Let's continue looking at how to create simple Windows Presentation Foundation (WPF) tools in PowerShell. This is a handy way to give users a graphical interface to run your scripts. WPF can be daunting as it is a complicated .NET developer topic. However, I have found that I can get by with a simple WPF form in many cases. It may not be the fanciest looking, but it is relatively easy to code. I want to show you how to create a stacked panel WPF form. As I mentioned last time, WPF is based on the idea of layers. You need a base window to begin with.

$window = [System.Windows.Window]@{

    Title                 = 'WPF Window Demo'

    Height                = 350

    Width                 = 500

    WindowStartupLocation = 'CenterScreen'

}
Don't forget you need to load the required assemblies first.
if ($IsWindows -OR $PSEdition -eq 'desktop') {

    Try {

        Add-Type -AssemblyName PresentationFramework -ErrorAction Stop

        Add-Type -AssemblyName PresentationCore -ErrorAction Stop

    }

    Catch {

        #Failsafe error handling

        Throw $_

        Return

    }

} else {

    Write-Warning 'This requires Windows PowerShell or PowerShell Core on Windows.'

    #Bail out

    Return

}
Instead of adding controls to the window, we're going to add them to a stack panel.
$stack = New-Object System.Windows.Controls.StackPanel
Let's see how to use this stack panel to create a simple form we can display using PowerShell.
Get a premium subscription for full article and archive access

Let's continue looking at how to create simple Windows Presentation Foundation (WPF) tools in PowerShell. This is a handy way to give users a graphical interface to run your scripts. WPF can be daunting as it is a complicated .NET developer topic. However, I have found that I can get by with a simple WPF form in many cases. It may not be the fanciest looking, but it is relatively easy to code. I want to show you how to create a stacked panel WPF form.

As I mentioned last time, WPF is based on the idea of layers. You need a base window to begin with.

$window = [System.Windows.Window]@{
    Title                 = 'WPF Window Demo'
    Height                = 350
    Width                 = 500
    WindowStartupLocation = 'CenterScreen'
}
Want to read the full issue?
GitHub Bluesky LinkedIn About Jeff
Powered by Buttondown, the easiest way to start and grow your newsletter.