UNDER CONSTRUCTION!!!

Tech News

Keeping You Up To Date With The Latest Tech News & Virus Threats
Font size: +

Intro to Microsoft Dialog Boxes in PowerShell

Intro to Microsoft Dialog Boxes in PowerShell

Recently, I have been exploring the limits of PowerShell scripting to understand its true potential. One of my experiments involved building my own version of Windows Notepad. In doing so, I used several premade dialog boxes from Microsoft, such as those for opening and saving files or selecting fonts.

The main reason for using Microsoft’s dialog boxes is that they saved me a ton of work. Sure, while you could create a font selection dialog box from scratch, it would require considerable effort. Why start from zero when Microsoft has already done the work for you? In this article, I will demonstrate how to leverage Microsoft’s dialog boxes in your scripts.

Creating a Basic GUI for PowerShell Dialog Boxes

To get started, you need to create a GUI-based script. I have written a simple GUI that we can use to test various dialog boxes:

Add-Type -AssemblyName System.Windows.Forms # Create the form $Form = New-Object Windows.Forms.Form $Form.Text = "Dialog Box Demo" $Form.Size = New-Object Drawing.Size(1024, 768) # Show the form dialog $Form.ShowDialog()

The first line of code loads the System.Windows.Forms assembly—and the line is absolutely crucial. All the various GUI elements exist within this namespace, so you can’t build a GUI without loading it. Additionally, Microsoft’s dialog boxes are stored in the System.Windows.Forms namespace. Therefore, you must load this assembly before you can use any of the dialog boxes.

The next code section defines a form, which, in this case, you can think of as a window. The block creates a Windows.Forms.Form object (basically an empty window), assigns a title to the window, and defines the window size. In this code, the window size is 1024 x 768 pixels, but you can adjust the dimensions as needed.

Related:How To Create an Interactive PowerShell Menu

The last line of code displays the form on the screen. The window remains hidden until this line executes.

Explore Microsoft Dialog Boxes

We have a simple GUI now, so let’s look at how to access the various dialog boxes provided by Microsoft. As noted earlier, all these dialog boxes are part of the System.Windows.Forms namespace.

In .NET, a namespace is a collection of classes. These classes, along with their associated methods, perform specific tasks. For example, the System.Windows.Forms namespace contains classes for creating checkboxes and displaying text boxes.

To use the System.Windows.Forms namespace to display a dialog box, the first step is to identify which dialog boxes are available and the name of each dialog box’s associated class. Microsoft provides a list of the classes within the System.Windows.Forms namespace:

https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms

Here are some commonly used dialog boxes included in this namespace:

ColorDialog

CommonDialog

FileDialog

FolderBrowserDialog

FontDialog

OpenFileDialog

PageSetupDialog

PrintControllerWithStatusDialog

PrintDialog

PrintPreviewDialog

SaveFileDialog

Related:How I Built My Own PowerShell Multi-File Search Tool

Let’s look at how to use one of these dialog boxes in your custom PowerShell script.

Example Dialog Box: ColorDialog Class

I will use the ColorDialog class for this article (just because ColorDialog is the first one on the list). However, the technique demonstrated works similarly for all the dialog boxes.

The first step in using a dialog box is to consult Microsoft’s documentation. You can find all the relevant information by going to the Microsoft page I linked to above and clicking on the link for the specific class you are interested in. For a shortcut, here is the URL for the ColorDialog class:

https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.colordialog

When you access the page (or any page for other classes), you will notice that Microsoft provides examples of how to use the class. Unfortunately, these examples are written in C#, not PowerShell, so you can’t incorporate the example code into your script. That’s OK, though, because displaying the dialog box is really easy. All it requires is a few lines of code.

Add-Type -AssemblyName System.Windows.Forms # Create the form $Form = New-Object Windows.Forms.Form $Form.Text = "Dialog Box Demo" $Form.Size = New-Object Drawing.Size(1024, 768) # Create a new instance of the ColorDialog $ColorDialog = New-Object System.Windows.Forms.ColorDialog # Show the form dialog $Form.Add_Shown({$ColorDialog.ShowDialog()}) $Form.ShowDialog()

The code block above is like the previous example, with the addition of two lines.

Related:Solving the PowerShell GUI Paradox (With Example Scripts)

The first new line creates a variable called $ColorDialog and sets it to equal an object of type System.Windows.Forms.ColorDialog. In other words, this variable contains the color dialog box.

The second new line uses $Form.Add_Shown to display the dialog box. It works in the same way as displaying the form. You can see the final result in Figure 1.

Dialog Box 1-1.jpg

Figure 1. Here is the color dialog box.

Having seen how to display a dialog box, let’s add some controls. In Part 2 of this series, I will demonstrate how to add code that allows you to open and close the dialog box programmatically. Then, in Part 3, I explain how to pass the selected color to PowerShell.

About the Author

Sign up for the ITPro Today newsletter

Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

Newsletter Sign-Up

(Originally posted by Brien Posey)
×
Stay Informed

When you subscribe to the blog, we will send you an e-mail when there are new updates on the site so you wouldn't miss them.

Cisco Patches Critical Vulnerability in Industrial...
PlayStation just lost a key console exclusive to X...
 

Comments

No comments made yet. Be the first to submit a comment
Already Registered? Login Here
Thursday, 07 November 2024

Captcha Image

I Got A Virus and I Don't Know What To Do!

I Need Help!