The script below shows how to create a simple WinForms GUI in PowerShell, where you can drag and drop files and folders and then process these with PowerShell commands when you click the button. I’ve pieced this together using various online source and my own trial and error, and figured it might be useful as a base for others who need to create an interface.
Tip: If you are unsure of control names or how to use properties on a control, remember that you can always launch Visual Studio with a WinForms project and then inspect the Properties window and Form1.Designer.cs file for “inspiration”.
Here is the script (or get it at GitHub).
### Sample showing a PowerShell GUI with drag-and-drop ### [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") ### Create form ### $form = New-Object System.Windows.Forms.Form $form.Text = "PowerShell GUI" $form.Size = '260,320' $form.StartPosition = "CenterScreen" $form.MinimumSize = $form.Size $form.MaximizeBox = $False $form.Topmost = $True ### Define controls ### $button = New-Object System.Windows.Forms.Button $button.Location = '5,5' $button.Size = '75,23' $button.Width = 120 $button.Text = "Print list to console" $checkbox = New-Object Windows.Forms.Checkbox $checkbox.Location = '140,8' $checkbox.AutoSize = $True $checkbox.Text = "Clear afterwards" $label = New-Object Windows.Forms.Label $label.Location = '5,40' $label.AutoSize = $True $label.Text = "Drop files or folders here:" $listBox = New-Object Windows.Forms.ListBox $listBox.Location = '5,60' $listBox.Height = 200 $listBox.Width = 240 $listBox.Anchor = ([System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Left -bor [System.Windows.Forms.AnchorStyles]::Right -bor [System.Windows.Forms.AnchorStyles]::Top) $listBox.IntegralHeight = $False $listBox.AllowDrop = $True $statusBar = New-Object System.Windows.Forms.StatusBar $statusBar.Text = "Ready" ### Add controls to form ### $form.SuspendLayout() $form.Controls.Add($button) $form.Controls.Add($checkbox) $form.Controls.Add($label) $form.Controls.Add($listBox) $form.Controls.Add($statusBar) $form.ResumeLayout() ### Write event handlers ### $button_Click = { write-host "Listbox contains:" -ForegroundColor Yellow foreach ($item in $listBox.Items) { $i = Get-Item -LiteralPath $item if($i -is [System.IO.DirectoryInfo]) { write-host ("`t" + $i.Name + " [Directory]") } else { write-host ("`t" + $i.Name + " [" + [math]::round($i.Length/1MB, 2) + " MB]") } } if($checkbox.Checked -eq $True) { $listBox.Items.Clear() } $statusBar.Text = ("List contains $($listBox.Items.Count) items") } $listBox_DragOver = [System.Windows.Forms.DragEventHandler]{ if ($_.Data.GetDataPresent([Windows.Forms.DataFormats]::FileDrop)) # $_ = [System.Windows.Forms.DragEventArgs] { $_.Effect = 'Copy' } else { $_.Effect = 'None' } } $listBox_DragDrop = [System.Windows.Forms.DragEventHandler]{ foreach ($filename in $_.Data.GetData([Windows.Forms.DataFormats]::FileDrop)) # $_ = [System.Windows.Forms.DragEventArgs] { $listBox.Items.Add($filename) } $statusBar.Text = ("List contains $($listBox.Items.Count) items") } $form_FormClosed = { try { $listBox.remove_Click($button_Click) $listBox.remove_DragOver($listBox_DragOver) $listBox.remove_DragDrop($listBox_DragDrop) $listBox.remove_DragDrop($listBox_DragDrop) $form.remove_FormClosed($Form_Cleanup_FormClosed) } catch [Exception] { } } ### Wire up events ### $button.Add_Click($button_Click) $listBox.Add_DragOver($listBox_DragOver) $listBox.Add_DragDrop($listBox_DragDrop) $form.Add_FormClosed($form_FormClosed) #### Show form ### [void] $form.ShowDialog()