This PowerShell script creates and displays a simple GUI window...

July 4, 2025 at 08:22 PM

Import-Module WinUIShell using namespace WinUIShell $win = [Window]::new() $win.Title = 'Hello from PowerShell!' $win.AppWindow.ResizeClient(400, 200) $button = [Button]::new() $button.Content = 'Click Me' $button.HorizontalAlignment = 'Center' $button.VerticalAlignment = 'Center' $button.AddClick({ $button.Content = 'Clicked!' }) $win.Content = $button # Activate() shows the window but does not block the script. $win.Activate() $win.WaitForClosed()

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:


  1. Import-Module WinUIShell

    • The script imports the WinUIShell module to access features like Window, AppWindow, and Button.
  2. using namespace WinUIShell

    • This command lets the script reference types from the WinUIShell namespace directly without needing to fully qualify them.
  3. $win = [Window]::new()

    • Creates a new instance of a Window object ($win), representing the main application window.
  4. $win.Title = 'Hello from PowerShell!'

    • Sets the title of the window to 'Hello from PowerShell!'.
  5. $win.AppWindow.ResizeClient(400, 200)

    • Resizes the client area of the window to a width of 400 pixels and a height of 200 pixels.
  6. $button = [Button]::new()

    • Creates a new button control and stores it in the $button variable.
  7. $button.Content = 'Click Me'

    • Sets the initial text label of the button to 'Click Me'.
  8. $button.HorizontalAlignment = 'Center'

    • Horizontally aligns the button to the center of the window.
  9. $button.VerticalAlignment = 'Center'

    • Vertically aligns the button to the center of the window.
  10. $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!'.
  11. $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.
  12. $win.Activate()

    • Displays the window on the screen. This method shows the window without blocking the continuation of the script.
  13. $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