Last active
March 20, 2025 18:59
-
-
Save dfinke/c338fc2f2463669e108162769813459c to your computer and use it in GitHub Desktop.
A PowerShell 7 function mimicking Out-GridView basics on Windows.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage: