This is not a tutorial article, just a few snippet and links to other scripts of mine that show (by example) how to make use of GUI widgets with Powershell.
Contents
Introduction & Other Resources
The main purpose of this post is to remind me/refresh my knowledge about this topic, because it’s been a while since I used WinForms or WPF/XAML extensively.
-
QuickADUserInfo: is a graphical Powershell script (using WPF/XAML) that performs Active Directory queries across multiple domains, to display basic information like […]
By the way: This was pretty much my first attempt of using Powershell with a GUI (initially with Windows Forms, but I soon switched to WPF).
And it was also an early Powershell script of mine in total; back then written for a (previous) day job, but also as a general learning experience and proving ground for my (back then: future, now current) job 😏 -
Additional GUI examples and demonstrations can be found in the powershell directory of my misc_public repository; its ReadMe.md has also screenshots.
Direct links to the scripts:
Examples & Snippets
Folder Browser Dialogue Box
Inspired by https://code.adonline.id.au/folder-file-browser-dialogues-powershell/
function Find-Folders
{
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
[System.Windows.Forms.Application]::EnableVisualStyles()
$browse = New-Object System.Windows.Forms.FolderBrowserDialog
$browse.SelectedPath = "C:\"
# or e.g.: $browse.RootFolder = [System.Environment+SpecialFolder]'MyComputer'
$browse.ShowNewFolderButton = $false
$browse.Description = "Select a directory"
$loop = $true
while ($loop)
{
if ($browse.ShowDialog() -eq "OK")
{
$loop = $false
# >>--> Insert your script here <--<<
}
else { return }
}
$browse.SelectedPath
$browse.Dispose()
}
Find-Folders
File Browser Dialogue Box (with Multiselect)
Inspired by https://code.adonline.id.au/folder-file-browser-dialogues-powershell/
Add-Type -AssemblyName System.Windows.Forms
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
Multiselect = $true # Multiple files can be chosen
Filter = 'Images (*.jpg, *.png)|*.jpg;*.png' # Specified file types
}
[void] $FileBrowser.ShowDialog()
$path = $FileBrowser.FileNames;
if ($FileBrowser.FileNames -like "*\*")
{
# Do something before work on individual files commences
$FileBrowser.FileNames #Lists selected files (optional)
foreach($file in Get-ChildItem $path)
{
Get-ChildItem ($file) | ForEach-Object { <# Do something to each file #> }
}
# Do something when work on individual files is complete
}
else { Write-Host "Cancelled by user" }
Show/Hide console with a mainly GUI-based script (Windows Forms)
Based on http://powershell.cz/2013/04/04/hide-and-show-console-window-from-gui/
Add-Type -Name Window -Namespace Console -MemberDefinition '[DllImport("Kernel32.dll")] public static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);'
function Show-Console ([bool] $show)
{
$consolePtr = [Console.Window]::GetConsoleWindow()
if ($show) { [Console.Window]::ShowWindow($consolePtr, 5) } # Show
else { [Console.Window]::ShowWindow($consolePtr, 0) } # Hide
}
Present data as a table via Out-GridView
Get-ChildItem | Out-GridView