Posted on Leave a comment

Building a Windows Form app with PowerShell in VS Code

This post uses PowerShell Pro Tools. PowerShell Pro Tools is a set of tools for PowerShell developers to build GUIs, package as executables and more.

PowerShell Pro Tools integration for VS Code provides the ability to generate Windows Forms PowerShell scripts that you can run in Windows PowerShell and PowerShell 7. Windows Forms code only runs on Windows and although PowerShell 7 runs on Linux and MacOSX, the Windows Forms libraries are not compatible.

Installing PowerShell Pro Tools

You’ll first need the PowerShell extension for Visual Studio Code installed. After installing the language extension, next install the PowerShell Pro Tools extension.

Install the Extension

After installing the extension, you will need a license. You can receive a trial license that is good for 21 days by invoking the PowerShell Pro Tools: Request Trial License command from the Command Palette (Ctrl+Shift+P).

Enter your email address and a trial license will be sent to your email within a couple minutes. The license will be attached as a text file to the email.

License Attachement

Open the license file and copy the entire text of the document. Issue the PowerShell Pro Tools: Install License Key command from the Command Palette (Ctrl+Shift+P). Paste the license key into the input box and press enter.

To view your license information after installing the license, issue the PowerShell Pro Tools: Display License Information command from the Command Palette (Ctrl+Shift+P).

License Information

Your extension is now installed and licensed.

Creating a Windows Form

To create a Windows Form, you first need to create a PS1 file. This file will be reponsible for the logic of your form. You can name the PS1 file whatever you wish.

After creating your PS1 file, invoke the PowerShell Pro Tools: Show Windows Form Design command from the Command Palette (Ctrl+Shift+P).

Designer is opening

The PowerShell Pro Tools Form Designer will open as a separate window.

PowerShell Pro Tools Form Designer

The form designer looks and behaves very similar to the form design in Visual Studio. On The left hand side is the designer canvas with the form component. On the right hand side is a collection of controls in the toolbox and a property grid for the currently selected control. At the bottom of the property grid, it shows the currently selected control’s name and type.

Building Your Form

To build you form, you can select a component from the Toolbox by left-clicking on it and then left-clicking on the form to drop the control where it was clicked. Dragging and dropping does not yet work for the toolbox.

After adding the control to the form it will be selected. As stated earlier, when a control is selected, the name and type appear below the control’s properties on the right hand side. Additionally, the Unsaved Form Indicator (*) will appear next to the file name of the designer file path.

Control Information

You can modify the control’s properties in the property grid. Changing the name will change the control’s variable that you will use in the PowerShell script.

Adding Form Logic

In order to add logic to your form, you will need to hook up event handlers to the controls. This can be done in two ways. The first way is to double-click the control you wish to add an event handler to. This will create the default event handler for the control. For a button, that is the onClick event handler. For a checkbox, that is the onChecked event handler. Each control will be different.

If you save and then switch back to the form.ps1 file in VS Code, you will see that the event handler has been created.

Double-click event handler

If you want to create event handlers that are not the default handler, you will need to open the event tab on the Properties Grid. This is the lightening bolt icon.

The property grid will now show all the event handlers for the control. You can see the onClick event handler we created by double clicking. You can enter the name of a new event handler in the textbox to the right of the event name to create it. Make sure to save after entering a name.

Event Property Grid

Packaging a Windows Form app

You can now use the packaging features of PowerShell Pro Tools to create an executable out of the two PS1 files that make up your form. The first step is to ensure that you have the correct dependencies installed for packaging. Since the packaging process uses the .NET Compiler to create the executable, you will need that installed.

Once your dependencies are installed, you can issue the PowerShell Pro Tools: Package as Executable command. Make sure to have your form.ps1 file selected before you do this.

After running this command, you will see output in the Terminal about the success or failure of the packaging process.

A successful package will look like this.

Successful Packaging

Once packaged, you can run the form.exe that was created. The form should show up, just as it would if you were to run the PowerShell script.

The running executable

In addition to creating the exe, a package.psd1 file will also be created to allow you to adjust the packaging settings. You can configure whether to hide the console window, if the application should run as administrator or if you need high DPI support enabled.

PowerShell Core Support

As mentioned earlier, the VS Code Form Designer only works on Windows and on Windows PowerShell or PowerShell 7. Since PowerShell 7 is built on .NET Core 3.0, it now has Windows Forms support. This means you can run the PowerShell Designer and produce forms run under PowerShell 7.

Windows Forms Running in PowerShell 7


In this post we went over how to create a Windows Form application with PowerShell Pro Tools for Visual Studio Code. For more information about packaging, visit out documentation site. To find out about licensing, please visit our store.

Posted on Leave a comment

PSAvalonia – Open source PowerShell bindings for Avalonia

Avalonia is a WPF-style cross-platform UI library. Today, we are open sourcing a PowerShell module to create UIs using the Avalonia library. The Avalonia bindings that were once part of PowerShell Pro Tools are now open source and up on GitHub and the PowerShell Gallery. You can download the latest version using Install-Module.

Install-Module PSAvalonia

You can contribute on GitHub here.

What can PSAvalonia do?

You can use PSAvalonia to create cross-platform UIs that work in PowerShell Core and PowerShell 7. It has been tested on 6.2 and 7. PSAvalonia does not work in Windows PowerShell at the moment.

For example, you could use the same script on both Linux and Windows.

$Xaml = '<Window xmlns=""
        mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
	<Button Width="160" Name="button">My Button</Button>
        <TextBox HorizontalAlignment="Left" Margin="12,12,0,0" Name="txtDemo" VerticalAlignment="Top" Width="500" Height="25" />
$window = ConvertTo-AvaloniaWindow -Xaml $Xaml
$Button = Find-AvaloniaControl -Name 'button' -Window $Window
$txtDemo = Find-AvaloniaControl -Name 'txtDemo' -Window $Window
$Button.add_Click({$txtDemo.Text = "Hello, World from $($PSVersionTable.OS) running PowerShell Core $($PSVersionTable.PSVersion)"})
Show-AvaloniaWindow -Window $Window
Running on Windows
Running on Linux

Posted on Leave a comment

PowerShell Tools 4.7.0 Release Notes

Support for High DPI Windows Forms applications

The packager now supports high DPI forms applications. Use the High DPI Support package setting to enable support. You can also enable this setting via the Merge-Script package config file.

High DPI Setting in Visual Studio

Fixed window flash on start and exit of packaged application

When hide console window is selected, no console will be shown at all. When you exit your application, a console used to flash. This is no longer the case.

Fixed app crash of packaged application

Occasionally, the application would crash when exiting due to a FileNotFoundException.

Posted on Leave a comment

PowerShell Tools for Visual Studio and PowerShell Pro Tools 4.6.2 – Release Notes

Hide Item Templates for non-PowerShell Projects

PowerShell item templates would show up in web projects and default to the first item in the list. This is not ideal because the project was primarily a C# project. PowerShell item templates will now only show up in PowerShell Projects.

Fix bundling of resource data files in some circumstances

If a resource file was added to a Windows Form by adding an icon, font or changing the language and then the resource file was deleted, the bundler would fail with a FileNotFoundException.

Fix installation issue on Visual Studio 16.1

Visual Studio 16.1 complains during installation of PowerShell Tools that the target .NET framework was installed when it was actually installed. Removed .NET version check until it can be determined how to correctly enforce it.

Posted on Leave a comment

PowerShell Pro Tools – 4.6.0 Release Notes

Event Handler Support for the PowerShell Windows Form Designer

The PowerShell Pro Tools form designer now supports generating event handler code. You can click the event tab on the properties grid to view events that are available for the currently selected control.

Events Tab

Event handlers that are already connected to script blocks will show the variable name in the text field. To generate a new event handler, type the name of the variable you’d like to use and hit enter. Once you save the form with Ctrl+s or by clicking the Save button in the toolbar, the event handler will be generated in the code.

Event handler code
Posted on Leave a comment

Video – PowerShell Pro Tools – Building Cross-Platform, WPF-Style UIs with PowerShell Core

In this video, I show how to build WPF-style applications with the Avalonia project. PowerShell Pro Tools offers bindings that make it easy to work with Avalonia in PowerShell Core. I also demonstrate running the exact same UI on both Windows and Linux.

Posted on Leave a comment

Building Cross-Platform WPF-Style Applications in PowerShell Core

With PowerShell Pro Tools 4.5.0, you can now take advantage of the Avalonia project in PowerShell Core. Avalonia is a WPF-inspired cross-platform XAML-based UI framework providing a flexible styling system and supporting a wide range of OSs: Windows (.NET Framework, .NET Core), Linux (GTK), MacOS, Android and iOS. PowerShell Pro Tools bridges the gap between PowerShell Core and Avalonia.

Avalonia is currently in beta so there may be some bugs. These cmdlets only work in PowerShell Core.

Creating a basic application

Simple to WPF, Avalonia uses XAML to define the look and feel of the application. Many of the control names and concepts are very similar. To create a basic Avalonia PowerShell application, you first need to define some XAML.

$Xaml = '<Window xmlns=""
        mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
	<Button Width="160" Name="button">My Button</Button>
        <TextBox HorizontalAlignment="Left" Margin="12,12,0,0" Name="txtDemo" VerticalAlignment="Top" Width="500" Height="25" />

This particluar form has a single textbox and button. They fall within a StackPanel. All these controls should seem very similar to WPF.
In order to create a new Avalonia window, you can use the PowerShell Pro Tools ConvertTo-AvaloniaWindow cmdlet to create a new window.

$window = ConvertTo-AvaloniaWindow -Xaml $Xaml

After creating a window, you may want to adjust the behavior of the button and textbox. To do so, you can find the control using the Find-AvaloniaControl cmdlet.

$Button = Find-AvaloniaControl -Name 'button' -Window $Window
$txtDemo = Find-AvaloniaControl -Name 'txtDemo' -Window $Window

Next, very similar to WPF, we can now add event handlers to the controls that we just found. This event handler is invoked when the button is clicked. It then sets the textbox’s value to “Hello, World from (Operating System) running PowerShell Core (Version)!”

$Button.add_Click({$txtDemo.Text = "Hello, World from $($PSVersionTable.OS) running PowerShell Core $($PSVersionTable.PSVersion)"})

Finally, we display the window using Show-AvaloniaWindow.

Show-AvaloniaWindow -Window $Window

If you run this script on Windows, you’ll see the following.

Running Avalonia on Windows

Here is the exact same script running from a Ubuntu Linux machine.

Running Avalonia on Linux

This functionality is part of the PowerShell Pro Tools module. This is also included with the PowerShell Pro Tools extension for VS Code.

Posted on Leave a comment

PowerShell Tools for Visual Studio and PowerShell Pro Tools – Version 4.4.0 Release Notes

PowerShell Pro Tools – Module Explorer

The PowerShell Pro Tools extension for VS Code now provides a new tree view in the activity bar to view and update modules. It will list all the modules that are currently installed and flag any modules that are out of date. You can then upgrade the module directly from VS Code.

Updating the Polaris Module with the Module Explorer

If you wish to hide the Module Explorer, just change the Module Explorer Visibility setting.

Improved Performance of VS Code Extension

Previously, the VS Code extension would execute all the PowerShell script necessary for its functions in the terminal window. Not only was this slow but it would pollute the history of the terminal. With the 4.4.0 release, the VS Code extension now uses a background runspace for operation. This improves performance and ensures a clean terminal history.

Improved Dependency Check Warnings

The packager requires both .NET Core and the .NET Framework Developer Pack installed to function. Previously, the warning messages were cryptic. These messages have been improved and contain links to where to download the missing dependencies.

Fixed an issue with the Form Designer Licensing

The form designer would reprompt for a license even after it had been installed in VS Code.