Skip to content

Instantly share code, notes, and snippets.

@dfinke
Last active March 20, 2025 18:59
Show Gist options
  • Save dfinke/c338fc2f2463669e108162769813459c to your computer and use it in GitHub Desktop.
Save dfinke/c338fc2f2463669e108162769813459c to your computer and use it in GitHub Desktop.
A PowerShell 7 function mimicking Out-GridView basics on Windows.
function Show-GridView {
param (
[Parameter(Mandatory, ValueFromPipeline)] $InputObject
)
begin {
Add-Type -AssemblyName System.Windows.Forms
$data = New-Object 'System.Collections.Generic.List[Object]'
}
process {
$data.Add([PSCustomObject]$InputObject)
}
end {
$form = New-Object System.Windows.Forms.Form -Property @{Width = 600; Height = 450 }
$grid = New-Object System.Windows.Forms.DataGridView -Property @{
Location = [System.Drawing.Point]::new(10, 10)
Size = [System.Drawing.Size]::new(560, 350)
SelectionMode = 'FullRowSelect'
MultiSelect = $true
}
$okButton = New-Object System.Windows.Forms.Button -Property @{
Text = 'OK'
Location = [System.Drawing.Point]::new(480, 370)
Width = 80
}
$okButton.Add_Click({
$selected = $grid.SelectedRows | ForEach-Object { $_.DataBoundItem }
$form.Tag = $selected
$form.DialogResult = 'OK'
$form.Close()
})
$cancelButton = New-Object System.Windows.Forms.Button -Property @{
Text = 'Cancel'
Location = [System.Drawing.Point]::new(390, 370)
Width = 80
}
$cancelButton.Add_Click({ $form.Close() })
$grid.DataSource = $data
$form.Controls.AddRange(@($grid, $okButton, $cancelButton))
if ($form.ShowDialog() -eq 'OK') {
return $form.Tag
}
}
}
@dfinke
Copy link
Author

dfinke commented Feb 25, 2025

Example usage:

$selected = Get-Process | Select-Object Name, ID, CPU | Show-GridView
if ($selected) { $selected | Format-Table }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment