In this post, we’ll look at how to build a PowerShell script that contains multiple Windows Forms in VS Code. You will need PowerShell Pro Tools to follow along with this tutorial.
First, we need to create the main form. Create a file named main.ps1 in VS Code. Once it’s created, click the Show Windows Forms Designer button at the top of the editor. Clicking the editor will open PSScriptPad with the Windows Forms Designer shown.
Now we will add two buttons to the form. The first button will open one form and the second another. I’ve added some custom text to separate them apart.
With some buttons created, we can start to create our other forms. Let’s create form1.ps1 and form2.ps1. Each form will have a single label on it to tell the form apart. Follow the steps from above. Create a file named file.ps1, open the Windows Forms Designer, add some controls and save the form. The resulting file structure will look like this.
After creating our forms, we will have to do a couple of things to make them work in a multi-form scenario. First, we need to update the form1.ps1 and form2.ps1 to remove the automatic opening of the form. This is accomplished via ShowDialog. We instead want this to happen when we click the button. As you can see, we removed the $form1.ShowDialog() portion of the script.
In addition to removing the automatic opening of a PowerShell form, we need to also update the form variable in the form1.designer.ps1 and form2.designer.ps1 (this step will not be required in a future version of PSScriptPad). Within the form file, replace all the $form1 references will a unique form name. For my forms, I used $subForm1 and $subForm2. This makes the form unique so that when we package them together to the other form, the variable doesn’t conflict.
Now, back in our main.ps1 file, we will want to update our event handlers to open our subforms. The first step is to dot source the subform files into the main.ps1 so the form variables are available. The second step is to call Show() on the form variable within the event handlers.
Now that we have all the files updated, we are ready to package them as an executable. The packaging process will automatically merge all the scripts together and include them within the resulting exe. You’ll want to make sure to have the PowerShell Pro Tools packager prerequisites installed before packaging. Aside from that, there shouldn’t be any other configuration necessary. You can just click the package button to produce an exe.
The packaging process output will be written to the terminal window. The process should only take about 10 seconds and the files will be written to an out folder within the same folder as your scripts. If you double-click your exe, you’ll see that the main window opens and that clicking either of the buttons will bring up the subform.
In this post, we looked at how to include multiple forms in a PowerShell tools executable. You can download PowerShell Pro Tools for Visual Studio Code from the Visual Studio Marketplace.