Loading…

Building Windows Forms with PowerShell in Visual Studio 2017

I recently released the first version of PowerShell Pro Tools for Visual Studio 2017. The first version provides only one feature; Windows Forms designer support. In this post we will go over how to create a Windows Form using PowerShell.

Installing the necessary tools

You will need the following:

Creating a Project

You can create a module or script project.

Click File->New->Project

Select the Module or Script project type, name it and then click Ok.

Create the Form

After installing the Pro tools, you should now have a Form item template available. Right click on your project and select Add->New Item.

Once the New Item dialog pops up, select the PowerShell Form template, name it and click Ok.

Working with the Form Designer

Adding Controls

The form designer works the same way with any language. You can select items from the Toolbox window and drag them onto your form. Properties of the controls can be set using the Properties window.

Adding controls automatically updates the Form.Designer.ps1 file. Do not edit this file by hand as the editor will simply recreate it after changes are made to the form.

Adding Event Handlers

To do anything interesting, you’ll need to add event handles. You can access a control’s events by selecting it in the designer and clicking the Event button in the Properties window.

Enter the name of your event handler function and click enter.

After you press enter, you will be moved into the code-behind view where you can wire up your event handler.

The event handler will automatically be wired up to your control.

Debugging A Form

Once you are ready to test out your form, you can click Start or press F5 from either the designer window or the code-behind window. PoshTools will fire off the script and you can set breakpoints and debug like any other PowerShell script.

And just like that you have a working Windows Form.

Conclusion

The PowerShell scripts generated by PoshProTools can be used in any PowerShell host. The designer and code-behind files can be joined into a single script. This is a manual process at the moment but I’m thinking about creating a “build” step that does this automatically.

Feel free to try PowerShell Pro Tools for free for a couple weeks. I would love to hear your feedback.

Stay tuned for new features in PowerShell Pro Tools. Next up: WPF.

4 thoughts on “Building Windows Forms with PowerShell in Visual Studio 2017

  1. Help, please

    I installed VS17 CE, then installed first PowerShellTools.15.0.vsix, then PowerShellToolsPro.vsix

    Everything looks good up to and including the point where I add controls to the form using the designer. However, even after saving the solution, the form ps1 file is not being updated with the form code. It stays looking like this:

    . (Join-Path $PSScriptRoot ‘Form1.designer.ps1’)
    $MainForm.ShowDialog()

    Thoughts?

    Thank you
    Bill Charlton

    1. Hi Bill,

      The code that manages the look and feel of the form is placed in a separate PS1 file in the same directory. The files name will be Form1.designer.ps1. The Form1.ps1 file is intended to handle event handlers and other logic. This is the same format that C# and VB.NET form designers utilize.

      I’ve had some thoughts on making them a single file or providing some way of merging them at “build” time.

      Let me know if that makes sense and that if you see the designer file.

      Thanks,
      Adam

  2. Bill Charlton again…. See my comment from earlier today

    Sorry, I forgot to say – I’m running Win 10/PS v5

    Name Value
    —- —–
    PSVersion 5.1.14393.1066
    PSEdition Desktop
    PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
    BuildVersion 10.0.14393.1066
    CLRVersion 4.0.30319.42000
    WSManStackVersion 3.0
    PSRemotingProtocolVersion 2.3
    SerializationVersion 1.1.0.1

Leave a Reply