This PowerShell script creates and displays a simple GUI window...
July 4, 2025 at 08:22 PM
This PowerShell script creates and displays a simple GUI window using WinUI features provided by the WinUIShell
namespace. Here’s what each part of the code does:
-
Import-Module WinUIShell
- The script imports the
WinUIShell
module to access features likeWindow
,AppWindow
, andButton
.
- The script imports the
-
using namespace WinUIShell
- This command lets the script reference types from the
WinUIShell
namespace directly without needing to fully qualify them.
- This command lets the script reference types from the
-
$win = [Window]::new()
- Creates a new instance of a
Window
object ($win
), representing the main application window.
- Creates a new instance of a
-
$win.Title = 'Hello from PowerShell!'
- Sets the title of the window to
'Hello from PowerShell!'
.
- Sets the title of the window to
-
$win.AppWindow.ResizeClient(400, 200)
- Resizes the client area of the window to a width of
400
pixels and a height of200
pixels.
- Resizes the client area of the window to a width of
-
$button = [Button]::new()
- Creates a new button control and stores it in the
$button
variable.
- Creates a new button control and stores it in the
-
$button.Content = 'Click Me'
- Sets the initial text label of the button to
'Click Me'
.
- Sets the initial text label of the button to
-
$button.HorizontalAlignment = 'Center'
- Horizontally aligns the button to the center of the window.
-
$button.VerticalAlignment = 'Center'
- Vertically aligns the button to the center of the window.
-
$button.AddClick({ $button.Content = 'Clicked!' })
- Attaches a click event handler to the button. When the button is clicked, the handler changes the button’s text content to
'Clicked!'
.
- Attaches a click event handler to the button. When the button is clicked, the handler changes the button’s text content to
-
$win.Content = $button
- Sets the button as the content of the window, effectively adding it as the main (and only) UI element in the window.
-
$win.Activate()
- Displays the window on the screen. This method shows the window without blocking the continuation of the script.
-
$win.WaitForClosed()
- Waits for the window to close. When the window is closed, the script will terminate or proceed past this line.
Output or Behavior:
- A window titled "Hello from PowerShell!" appears with a single button in the center labeled "Click Me".
- When the button is clicked, its label text changes to "Clicked!".
- The script waits until the user closes the window.
This demonstrates creating a basic graphical user interface (GUI) application in PowerShell using the WinUIShell
module.
Generate your own explanations
Download our vscode extension
Read other generated explanations
Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node