A high-performance, professional-grade Windows Forms (WinForms) template for PowerShell developers. This script provides a fully functional GUI scaffold that solves the most common problem in PowerShell GUI development: the UI freezing during execution.
- 🚀 Multi-Threaded Engine: Uses
RunspacePoolto execute heavy logic in the background, ensuring the user interface remains smooth and interactive. - 🧠 Intelligent CPU Scaling: Automatically detects the host machine's processor count (
$env:NUMBER_OF_PROCESSORS) to optimize the thread pool size. - 💬 Real-Time Color Logging: A built-in
RichTextBoxoutput window supports color-coded logging (Green for success, Red for errors) to keep the user informed. - 📂 Smart File Integration: Includes a robust file browser for
.txtand.csvfiles, complete with automatic line counting and validation. - 🛠 Thread-Safe Communication: Utilises a
[hashtable]::Synchronizedobject to allow background threads to safely update the UI (progress bars, text boxes, etc.) without causing race conditions. - 🎄 Seasonal Easter Egg: The GUI features a dynamic theme! Between December 25th and January 2nd, the UI automatically switches to a festive "Merry Christmas" mode with a custom color scheme.
This template is designed for stability and performance using the following components:
| Component | Purpose |
|---|---|
| RunspacePool | Manages a collection of background threads to prevent UI blocking. |
| Synchronized Hashtable | Acts as a bridge between the Background Thread and the UI Thread. |
| SessionStateVariableEntry | Injects the shared hashtable into the background runspace for seamless data passing. |
| WinForms | Provides the graphical interface (Buttons, Progress Bars, Menus). |
To use this as a template, simply replace the contents of the $scriptRun block with your own code:
$scriptRun = {
# Your heavy-duty logic goes here
# Use $hash.outputBox to log messages
# Use $hash.progressBar1 to update progress
$hash.OutputBox.AppendText("`r`nStarting task...")
}