Dynamic/Active Checkbox in Powershell GUI

As part of a Powershell GUI script that I wrote, I had to dynamically change the content of a listbox, based on a checkbox selection.
In this blog, I will review a sample code that will allow you to do so in the case will need to use this kind of option in one of your scripts.

The GUI Form

The script is very basic and its only propose is to emphasize the main parts of the dynamic checkbox actions inside the script.

The script is very simple and it contains a listbox, checkbox, and a button.
When you run the script, it loads the list of all service’s names on the machine:

If you select the checkbox Include Service Status the listbox will dynamically be changed and will list each service name and status:

The script main components

There 2 major components in the script to achieve a dynamic checkbox

  • The initial list of services which will be loaded into the listbox with services names only.

Line #85: Get the services list into $Getsvc
Line #86: Add the list from $Getsvc to the listbox $Getsvclist
Line #87: Select automatically the first item (service name) from the listbox $Getsvclist

  • The dynamic state of the checkbox which will refresh the list box with the right parameters as selected; service name with or without the status.

By the way, the second part could be even shorter and simpler, by removing the wait command for the mouse cursor (lines 100 & 111) and displaying the service status in a less friendly way (lines 104–106):

Line #90: Check if the checkbox state was changed
Line #91: Clear the listbox before loading the new data based on the checkbox selection.
Line #92/93: In case the checkbox $ACTcheckBox was checked, $Getsvc will contain the service name AND status.
Line #94: In case the checkbox $ACTcheckBox was NOT checked, $Getsvc will contain the service name only.
Line #95: Add the content of $Getsvc to the listbox $Getsvclist.
Line #96: The first object from the list will be selected automatically.
Line #97: End of the procedure.

The Script

During the last 13 years, I'm working as a Senior Customer Succes Engineer (former PFE) at Microsoft. My areas of expertise are Exchange, Powershell & Azure.