PowerShell WinForms interactive Prototyping/Debugging

Credits:

Motivation

  • PowerShell’s interactive nature lends it to be a handy way to experiment with .Net objects… including UI elements like WinForms/WPF…
  • Yet firing up UI’s classically take over the active thread to service the user interaction (e.g. mouse events etc)…
  • Without the extra gravy below, our otherwise handy interactive powershell locks up tight until we close down the Windows Forms application thus releasing the main thread back to the command line
  • The following gravy throws a Windows Form onto a background thread such that we can continue to manipulate the UI objects WHILE THEY’RE RUNNING, yay!

The Gravy

create RunSpaceWinForm.ps1 as such

and here is simple usage… spin up a running WinForm and give control back to the powershell prompt

Breakpoints

  • by default, breakpoints will now message as: “WARNING: Breakpoint Line breakpoint on ‘xyz’ will not be hit”
  • the above run will kick out something like the following…

  • execute that debug-runspace command and your breakpoints will light up in the editor and break when the code fires
  • CTRL-BREAK your way out of that mode when you want to go back to the powershell command line and manipulate the UI objects some more