In this post, we will look at how to build Windows Forms in the Windows PowerShell ISE. This technique takes advantage of the PowerShell Pro Tools PowerShell Module.
First, install the PowerShell Pro Tools PowerShell Module.
Next, you will need to either purchase a license or request a trial license to run the PowerShell Windows Form Designer. You can install your license using Install-PoshProToolsLicense.
Now, let’s create a couple of add-on commands within the ISE to make it easier to open the Windows Form Designer and reload a form when it changes. The first command we can create is used to open the Windows Form Designer. You can use the Show-WindowsFormDesigner cmdlet from PowerShell Pro Tools to open the form designer. Let’s use this and create a new add-on command. Add-on commands will appear within the add-on menu in the ISE’s menu bar.
The command uses the $psISE variable to retrieve the current file and then pass that file to the Show-WindowsFormDesigner cmdlet. It also passes the path to a designer.ps1 file that the designer will use to save the design code.
Now you can try and create a new Windows Form file. In this example, I’m creating a file named form.ps1 and putting some startup code within the file.
You can use the Windows Form Designer to add new components to your form, modify properties and add event handlers. All the code that is generated will be added to the form.designer.ps1 file. When you add an event handler, a variable will be set to a script block within the form.ps1 file and that variable will be assigned to the event handler within the form.designer.ps1. One of the issues with the ISE is that it doesn’t reload files when they are modified outside of the ISE. Thus, we can add a new add-on command that will reload the form.ps1 file after it has been modified within the Windows Form Designer.
Now when you make changes to the event handler code in the Windows Form Designer, you can reload the form.ps1.
In this post, we went over how to use the PowerShell Pro Tools Windows Form Designer in the PowerShell ISE. You can learn more about PowerShell Pro Tools here.