RSS

#PowerShell – Create a simple GUI form in seconds

01 Jun

As a PowerShell person I’m personally not a big fan of creating a GUI for PowerShell scripts. This is however something that is asked a lot when PowerShell scripts are created for by example provisioning new users.

I’ve seen many different approaches to this. Most worked around it either by using Read-Host or creating a GUI inside their function. In some cases these approaches also meant that the function could not handle pipeline input, multiple inputs, etc. So basically they automated one thing, but prevented further automation by implementing the GUI in a stupid way.

Personally I prefer to keep my functions without a GUI. If it is desired or required, the GUI code will be written outside of the function and will result in the calling of the function with the values specified in the GUI. This way the GUI part will not pollute the function and users have more choice and flexibility to achieve their goal. You could compare it to how you normally handle output. You output objects and let users handle the formatting instead of providing them with by example the output in Format-Table.

Now the approach is clear, the GUI still needs to be created. There are multiple ways to go about this. You could by example create the GUI by looking up what exactly you need to code to get the desired result or you could use (paid) 3rd party tools / services. You could by example use the free web-based service POSHGUI to visually create your form and then have it output your needed code. There are however also (paid) editors like Visual Studio or PowerShell Studio. Another very simple method in some cases is using Out-Gridview with -PassThru. For more info, see also my previous blog post Summary of 2nd Dutch PowerShell User Group – DuPSUG meeting with additional resources

In my case I created my initial GUI form with POSHGUI. Even though you can do many more advanced GUI forms, I soon realized that I was always creating the same type of GUI form that consisted of a couple of text boxes and a submit button. Creating these manually become an annoyance very quickly. That’s why I decided to create a function to dynamically create such a GUI based on my input.

The result is this:

The code itself can be found on my GitHub: https://github.com/bjornhouben/PowerShell/blob/master/General/GUI/New-SimpleGUIForm.ps1

I hope you like it and find it useful.

Advertisement
 
5 Comments

Posted by on June 1, 2018 in Automation, ICT, Microsoft, Powershell

 

Tags: , , , , , ,

5 responses to “#PowerShell – Create a simple GUI form in seconds

  1. Lee

    June 1, 2018 at 16:45

    Thanks for sharing Bjorn. Deffinatly something I can use.

    Like

     
    • Bjorn Houben

      June 1, 2018 at 18:09

      Good to hear. Thanks for leaving a comment. It’s Motivation to keep on creating new blog posts.

      Like

       
      • Raymond

        July 24, 2019 at 08:02

        Hi Bjorn,

        Would it be possible to save the input to a file.
        I need to collect for example user e-mail adres and wifi-mac addresses.
        I would like to have all imput to csv file.

        Hope to hear from you

        Like

         
        • Bjorn Houben

          July 25, 2019 at 12:51

          It is possible, but it might be better to explain your purpose in more detail because I think there might be better ways.

          Like

           

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

 
%d bloggers like this: