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

Conclusion

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

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

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

Fixed an issue with parameters passed to packaged scripts

A change made to the $PSScriptRoot support broke the ability to use a param() block in a packaged script. This has been resolved and you should be able to pass parameters to a packaged script again.

Fixed an issue with the installer cmdlets missing dependencies

The Wix binaries were not being installed with the PowerShell Pro Tools module. When attempting to run New-Installer, it would fail and complain about missing candle.exe or light.exe.

Improved the First Time User Experience with the VS Code Forms Designer

The VS Code forms designer will now insert code into the primary form file to help get started with running the form. This includes loading the System.Windows.Forms assembly, dot-sourcing the designer file and calling ShowDialog() on the form.