avatarShalitha Suranga

Summary

This context provides instructions on how to enhance Windows batch files by adding a graphical user interface (GUI) using PowerShell and .NET.

Abstract

The article "How to Enhance Your Windows Batch Files by Adding GUI" explains how to modernize old-fashioned Windows batch files using PowerShell and .NET. It starts by explaining the benefits of PowerShell scripting over traditional command prompt applications, including object-based output, pipelines, and new commands. The author then provides examples of how to execute PowerShell code blocks within a batch script to enhance usability for non-technical users. These examples include displaying toast notifications, message boxes, input boxes, and custom GUI elements using .NET methods. While this approach may slow down batch files slightly, the author argues that modern computers have enough computational power to handle the extra processes required for GUI elements.

Bullet points

  • The article provides instructions on how to modernize old-fashioned Windows batch files using PowerShell and .NET.
  • PowerShell has several advantages over traditional command prompt applications, including object-based output, pipelines, and new commands.
  • Toast notifications can be added to batch scripts to show the completion of a task or a long-running process.
  • Message boxes can be used for displaying error messages, information regarding tasks, and asking decisions from the user.
  • Input boxes can be used to capture input from the user to a variable in a more user-friendly manner than using the set command.
  • Custom GUI elements can be implemented using .NET methods in PowerShell.
  • The approach of adding GUI elements to batch files using PowerShell and .NET may slow down the script slightly, but modern computers have enough computational power to handle the extra processes required.

How to Enhance Your Windows Batch Files by Adding GUI

Let’s modernize your old-fashioned Windows batch files with the help of PowerShell and .NET

Photo by Bahman Adlou on Unsplash

We normally use Bash scripting on the Linux platform to perform various types of automation tasks. Similarly, we could write batch scripts on Windows. However, Microsoft introduced PowerShell scripting with the new versions of Windows by adding a way to use the modules of the .NET framework. PowerShell is having some advanced features such as object-based output, pipelines, and a lot of new commands compared to the old command prompt application. If you have several Bash scripts on Linux, commands such as zenity and notify-send can be used to modernize those scripts by adding graphical elements.

A few days ago, I was adding a new feature to Neutralinojs to display toast notifications on Windows. I had several options: implementing by using a library like WinToast, implementing from scratch, and achieving the same result by starting a process of PowerShell with a code-block. The third option was so quick, and the same concept can be applied to modernize old-fashioned Windows batch files too. If we write a batch file for a non-technical audience, it is always nice to use some GUI to enhance usability.

Basics

The following command format is used to execute a code-block on PowerShell silently. Also, it could be added into any batch script to execute PowerShell snippets. Therefore, if the user is running the batch script on a Windows version that supports PowerShell scripting, the specific code-block will be executed in PowerShell via the command prompt application.

powershell -Command "& {<PowerShell code-block goes here>}"

Notifications

Windows toast notification is a great way to show the completion of a task or a long-running process instead of printing some text in the console.

If the above batch script is executed, the following type of toast notification will be displayed.

A toast notification is created from a batch script, a screenshot by the author.

Message boxes

Native GUI-based message boxes can be used for displaying error messages, information regarding tasks, and also for asking decisions from the user in a user-friendly manner. If we use old-fashioned batch script commands, we often have to ask the user to type a specific key for each decision. The following batch script will display a basic message box.

The above batch script source code will make a graphical information message box as shown below. Also, other message box types such as warning, error, question, etc can be displayed.

A message box is created from a batch script, a screenshot by the author.

Furthermore, we can also detect which button was clicked by saving the output from the PowerShell interpreter to a batch script variable. But, batch scripting is not supporting direct assignments like Bash scripting on Linux platforms. Therefore, the easiest way is to create a temporary file to hold the output value. The following batch script will display a message box with a “Yes” button and a “No” button. Thereafter, according to the button that was clicked by the user, it will perform later actions accordingly.

Input boxes

The set command can the used to capture input from the user to a variable. But usability will be improved for nontechnical users if the input is captured from a GUI-based textbox because it will support basic features coming from the operating system such as undo, redo, and copy-paste. Let’s write a batch script to display a captured user input entirely using GUI elements.

The above script will display an input box. Thereafter, it will show the entered text via a native message box as shown in the following screen recording.

An example of a native input box, a screen recording by the author.

Custom GUI elements

All the above demonstrations use pre-built UI elements. Even though you need to build custom dialog boxes, it is possible to implement with PowerShell commands because it offers bindings to .NET methods. Anything accomplished with .NET also could be implemented in a batch script via PowerShell code-blocks using this approach. Moreover, it is possible to use this approach to display native GUI components and notifications via any programming language as I did by using C++ for the Neutralinojs project.

The above script will show a window with a native label. Likewise, any custom GUI element can be added to your Windows batch files by calling required methods from the .NET framework.

An example of a simple custom GUI, a screenshot by the author.

There is a minor issue with this method. This approach may slow down your batch files a bit because each GUI element such as a parent window or notification will create a separate process. However, that slowness can be ignored with the computation power of modern computers.

Programming
Windows
Technology
Software Development
Powershell
Recommended from ReadMedium