Looking for a simple alternative to editing PowerShell in VS Code? Check out PSScriptPad!
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.
After installing the extension, you will need a license. You can get a license key through our website.
To install your license key, 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).
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 responsible 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).
The PowerShell Pro Tools Form Designer will open as a separate window.
The form designer looks and behaves very similar to the form design in Visual Studio. 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 your 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.
You can modify the control’s properties in the property grid. Changing the name will change the control 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.
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 lightning 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.
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.
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.
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.
Building a Multi-Form Application
To build a multi-form application using PowerShell, read our follow up post.
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 our documentation site. To find out about licensing, please visit our store.