Last active
October 24, 2018 11:22
-
-
Save talatham/8259d3f91a6fb8cb3c9cfb15a76eba44 to your computer and use it in GitHub Desktop.
Export a PowerShell datagrid to a CSV file
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 export-DGV2CSV ([Windows.Forms.DataGridView] $grid, [String] $File) | |
<# | |
.SYNOPSIS | |
Export basic datagrid to CSV file | |
.PARAMETER grid | |
Datagrid object | |
.PARAMETER file | |
Path to CSV file | |
#> | |
{ | |
if ($grid.RowCount -eq 0) { return } # nothing to do | |
$row = New-Object Windows.Forms.DataGridViewRow | |
$sw = new-object System.IO.StreamWriter($File) | |
#Write header line | |
$sw.WriteLine( ($grid.Columns | % { $_.HeaderText } ) -join ',' ) | |
#Export contents | |
$grid.Rows | % { | |
$sw.WriteLine( | |
($_.Cells | % { $_.Value }) -join ',' | |
) | |
} | |
$sw.Close() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment