---
title: How to Craft a Modern PowerShell Message Box
description: Discover all the modern ways to craft a PowerShell Message Box and get away from the VBScript methods of old in this updated how-to!
canonical: https://adamtheautomator.com/powershell-message-box/
---

# How to Craft a Modern PowerShell Message Box

> Discover all the modern ways to craft a PowerShell Message Box and get away from the VBScript methods of old in this updated how-to!

Source: https://adamtheautomator.com/powershell-message-box/

---

- How to Craft a Modern PowerShell Message Box Tap to hide Home

- Tutorials

- Guidebooks

- Instructors

- Get Paid to Write

- Advertising

- Recommended Resources

- About Adam

                Search for:

-

-

-

-

# How to Craft a Modern PowerShell Message Box

        Published:2 January 2023 - 5 min. read

- PowerShell

          [](https://adamtheautomator.com/author/edem-afenyo/)

            [Edem Afenyo](https://adamtheautomator.com/author/edem-afenyo/)

            Read [more tutorials](https://adamtheautomator.com/author/edem-afenyo/) by Edem Afenyo!

-

        [](https://specopssoft.com/product/specops-password-auditor/?utm_source=adamtheautomator&utm_medium=referral&utm_campaign=adamtheautomator_referral_na&utm_content=display)
        Audit Active Directory for stale users, weak passwords, and other security risks with [Specops Password Auditor](https://specopssoft.com/product/specops-password-auditor/?utm_source=adamtheautomator&utm_medium=referral&utm_campaign=adamtheautomator_referral_na&utm_content=text).

Table of Contents

- Prerequisites
- Crafting a PowerShell Message Box via the Terminal.Gui
- Creating a PowerShell Message Box with AvaloniaInstalling Avalonia
- Creating a Message Box
- Conclusion

                XFacebookLinkedIn

Are you looking for a graphical way to notify users of your scripts or gather feedback? The world has changed since the good old days of [Windows.MessageBox](https://ss64.com/ps/messagebox.html)-based message boxes. But luckily, a modern PowerShell message box is up to the task. How?

In this tutorial, you will learn two of the newer and more powerful tools you can use to generate a PowerShell message box, [Terminal.Gui](https://github.com/gui-cs/Terminal.Gui) and [Avalonia](https://avaloniaui.net/). Between these two tools, you can not go wrong.

Read on and craft message boxes that users simply cannot ignore!

## Prerequisites

This tutorial will be a hands-on demonstration. To follow along, be sure you have the following:

- PowerShell 7+ – This tutorial uses PowerShell 7.3.

Related:[PowerShell 7 Upgrade : A How to Walk Through](https://adamtheautomator.com/powershell-7-upgrade/)

- A Fedora machine – This tutorial uses Fedora 35 (with PowerShell installed) to create and launch the Terminal.Gui-based message box, though this also will work in Windows as well.

Related:[Getting Advantage of PowerShell on Linux: A Beginner’s Guide](https://adamtheautomator.com/powershell-linux/)

- A Windows machine – This tutorial uses Windows 10 to create and launch the Avalonia-based message box

## Crafting a PowerShell Message Box via the Terminal.Gui

The Terminal.Gui tool provides Terminal User Interfaces (TUI), which lets you create a PowerShell message box using the terminal. But before creating a PowerShell message box, you must first install Terminal.Gui on your system.

1. Log in to your Fedora machine, open the terminal, and run the pwsh command below to access the PowerShell prompt.

pwsh

Accessing PowerShell via terminal

2. Next, execute the [Install-Module](https://learn.microsoft.com/en-us/powershell/module/powershellget/install-module) cmdlet below to install the [ConsoleGuiTools](https://www.powershellgallery.com/packages/Microsoft.PowerShell.ConsoleGuiTools) module. This module gives you access to the Terminal.Gui tool in PowerShell.

Install-Module Microsoft.PowerShell.ConsuleGuiTools

Type Y and press Enter when prompted, as shown below, to trust the source repository of the module and start the installation.

Installing Terminal.Gui

3. Once installed, create a new file in your favorite text editor, populate the following script into the file, and save it. You can name the script as you like, but this tutorial’s choice is prompt-test.ps1.

The code below tells PowerShell to launch a message box with the Query method of the Terminal.Gui.MessageBox object. Query, which is the focal point of the code below, takes at least three arguments, as follows:

Query(title, message, buttons)

ArgumentsDescriptionTitleThe message box's title is set to “Important” for this tutorial.MessageThe text to display to the user of your application. For this tutorial, the message is set to “Do you love PowerShell?”.ButtonThe names of the buttons in the message box, in this case, Yes and No. Each will be assigned an internal numerical value (0 and 1) based on its position. That value will be returned to the script for onward processing.

# Import the module
Import-Module Microsoft.PowerShell.ConsoleGuiTools

# Load the Terminal.Gui assembly
$module = (Get-Module Microsoft.PowerShell.ConsoleGuiTools -List).ModuleBase
Add-Type -Path (Join-path $module Terminal.Gui.dll)

# Initialise Terminal.Gui
[Terminal.Gui.Application]::Init()

# Create a message box and assign the result of a selection to a variable
$result = [Terminal.Gui.MessageBox]::Query("Important", "Do you love PowerShell?", @("Yes", "No"))

# Utilise the result of the selection
# Write output to the terminal window and close the message box
if ($result -eq 0)
{
    # Shutdown the GUI application gracefully
    [Terminal.Gui.Application]::shutdown()
    write-host("It's good you love PowerShell!")
}
if ($result -eq 1)
{
    # Shutdown the GUI application gracefully
    [Terminal.Gui.Application]::shutdown()
    write-host("Too bad, PowerShell is great. Give it a chance.")
}

💡 Besides creating a script, note that you can invoke a message box or other visual user interface elements directly from the prompt.

4. Now, switch to your PowerShell prompt, and run the below command to execute the script (*prompt-test.ps1*) from the working directory (.)

Related:[How to Run a PowerShell Script From the Command Line and More](https://adamtheautomator.com/run-powershell-script/)

./prompt-test.ps1

If the script works, a small message box appears on your screen, as shown below.

Viewing the message box

5. Lastly, select an option, Yes or No, and see what response you will get in your PowerShell prompt. You can use the mouse or the keyboard for navigation.

Selecting a message box button

Based on your chosen option, you will have the appropriate response in your PowerShell prompt, as shown below.

Viewing the result of selecting a message box option

## Creating a PowerShell Message Box with Avalonia

If you are more into seeing a Windows message box, Avalonia is an excellent option. You will generate a message box using Avalonia, which provides a full-blown GUI window.

But first, you must register [NuGet](https://www.google.com/search?q=nuget&oq=nuget&aqs=chrome..69i57.602j0j1&sourceid=chrome&ie=UTF-8) as a package source. Avalonia and other necessary packages will be downloaded through NuGet.

Log in to your Windows machine, open [PowerShell as administrator](https://adamtheautomator.com/powershell-run-as-administrator/), and run the following command. This command registers ([Register-PackageSource](https://learn.microsoft.com/en-us/powershell/module/packagemanagement/register-packagesource)) NuGet as a package source under MyNuget (or any name you prefer).

Register-PackageSource -Name MyNuGet -Location <https://www.nuget.org/api/v2> -ProviderName NuGet

Registering NuGet as a package source

### Installing Avalonia

Before taking advantage of Avalonia, like Terminal.Gui, you first have to install Avalonia on your machine.

1. Execute the [Install-Package](https://learn.microsoft.com/en-us/powershell/module/packagemanagement/install-package) cmdlet below to install the [netstandard.library](https://www.nuget.org/packages/NETStandard.Library/) package for your user account only. This package is a dependency for Avalonia.

The command below turns on the -SkipDependencies switch to avoid errors with non-critical netstandard.library dependencies.

Install-Package -Name netstandard.library -Scope CurrentUser -SkipDependencies

Type A and press Enter to confirm the installation when prompted, as shown below.

Installing Avalonia dependencies

2. Next, run Install-Package to install the avalonia package for the CurrentUser only. Installation for the current user allows you to carry on without an elevated command prompt.

Install-Package -Name avalonia -Scope CurrentUser

Confirm the installation when prompted, as shown below, and you will see a list of dependencies and packages installed along the way.

Installing Avalonia

3. Now, run the below command to install the PSAvalonia package, which provides direct PowerShell bindings for Avalonia.

Install-Package -name psavalonia -scope CurrentUser

Installing the PSAvalonia package

### Creating a Message Box

With Avalonia and its dependencies installed, you can create a modern PowerShell message box. You will create a PowerShell script that functions similarly to your script for Terminal.Guit o create your message box.

1. Create a new file, add the code below, and save it as a PowerShell script. You can name the script as you prefer, but this tutorial uses msgbox.ps1 as the script name.

This code functions similarly to your script for Terminal.Gui, but this time, the message box is designed via XAML.

# Design a message box
$Xaml = '<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d" d:DesignWidth="300" d:DesignHeight="200"
		Width="300"
		Height="200"
        x:Class="avaloniaui.MainWindow"
        Title="Important">
   <StackPanel>
	<TextBlock Margin="10" Name="msg" VerticalAlignment="Top" Width="200" Height="25"> Do you love PowerShell? </TextBlock>
	<Button Width="160" Name="button1">Yes</Button>
	<Button Width="160" Margin="10" Name="button2">No</Button>

    </StackPanel>
</Window>'

# Convert XAML description to Avalonia object
$window = ConvertTo-AvaloniaWindow -Xaml $Xaml

# Map Avalonia controls to PowerShell variables
$Button1 = Find-AvaloniaControl -Name 'button1' -Window $Window
$Button2 = Find-AvaloniaControl -Name 'button2' -Window $Window

# Handle responses once the message box is loaded.
# Write to the terminal and close the message box.
$Button1.add_Click({
	write-host("It's good you love PowerShell!")
	$window.close()
	})
$Button2.add_Click({
	write-host("Too bad, PowerShell is great. Give it a chance.")
	$window.close()
	})

# Show message box
Show-AvaloniaWindow -Window $Window

2. Next, run the pwsh command below to execute your script (msgbox.ps1).

pwsh .\msgbox.ps1

3. Now, select any option from the message box that appears.

Selecting an option from the message box

Based on your chosen option, the appropriate response is returned to the terminal like the one below.

Verifying the returned response

## Conclusion

The skills in creating PowerShell message box come in handy with your PowerShell scripts, whether to make interactive tasks or notify users. And in this tutorial, you have discovered tools (Terminal.Gui, and Avalonia) to craft a modern PowerShell message box.

Why not get further acquainted with either of the tools you learned about in this tutorial?

With Terminal.Gui, try [adding extra elements like checkboxes and menus](https://blog.ironmansoftware.com/tui-powershell/#menus) to your message box. Or perhaps, visually design the message box with [Ironman Software’s Terminal GUI designer.](https://github.com/ironmansoftware/terminal-gui-designer) For Avalonia, you can play around with more controls, [such as textboxes, combo boxes, and calendars.](https://docs.avaloniaui.net/docs/controls)

  Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

  [Explore ATA Guidebooks](https://adamtheautomator.com/ata-guidebooks/)

## More from ATA Learning and Partners

- ### Recommended Resources! Recommended Resources for Training, Information Security, Automation, and more!

- ### Get Paid to Write! ATA Learning is always seeking instructors of all experience levels. Regardless if you’re a junior admin or system architect, you have something to share. Why not write on a platform with an existing audience and share your knowledge with the world?

- ### ATA Learning Guidebooks ATA Learning is known for its high-quality written tutorials in the form of blog posts. Support ATA Learning with ATA Guidebook PDF eBooks available offline and with no ads!

## Categories

- IT Ops

- Cloud

- DevOps

- Home Ops

- Information Security

- Software Development

## Site

- Home

- Tutorials

- Guidebooks

- Instructors

- Get Paid to Write

- Advertising

- Recommended Resources

- About Adam

        Copyright 2026&copy; ATA Learning | [Privacy Policy](https://adamtheautomator.com/privacy/)

                                                        Don't be left behind with the ATA Learning Newsletter!

Looks like you're offline!
