Exporting ListView data to CSV

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 6 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
Tmartin113
Posts: 2
Last visit: Mon Oct 19, 2020 1:53 pm

Exporting ListView data to CSV

Post by Tmartin113 »

Hello,

I am using PowerShell Studio 2017. I currently have a list view with 5 columns that auto fill when i launch the form. I am needing to export all of the data in that list view to a CSV file and have it formatted exactly like the list view.

It looks like this:

Username | Associate | Title | Manager | Department

Here is how the list view gets populated:

Code: Select all

$searcher = [adsisearcher]"(samaccountname=$Content)"
foreach ($DirectReportDN in $searcher.FindAll().Properties.directreports)
{
    $DirectReport = [adsi]"LDAP://$DirectReportDN"
    $Username = $DirectReport.Properties.samaccountname
    $Associate = $DirectReport.Properties.name
    $Title = $DirectReport.Properties.title
    $Mgr = $DirectReport.Properties.extensionattribute6
    $Dept = $DirectReport.Properties.department

    Add-ListViewItem -ListView $listview2 -Items $Username -Group $listview2.Groups[0] -SubItems "$Associate", "$Title", "$Mgr", "$Dept"
}
I have tried doing

Code: Select all

$listview2.Items | Export-Csv -Path C:\Temp\Test.csv -NoTypeInformation

Code: Select all

$listview2.Items.Text | Export-Csv -Path C:\Temp\Test.csv -NoTypeInformation
and

Code: Select all

$listview2.Items[0..5].Text | Export-Csv -Path C:\Temp\Test.csv -NoTypeInformation
I even tried to create a while loop to iterate through the items and subitems of the list view, but to no avail.

Nothing has been working so far and i have not been able to find a reliable solution. Any help would be appreciated. Thanks.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Exporting ListView data to CSV

Post by jvierra »

You cannot directly export from a ListView. Use a DataGridView to export to a CSV.
User avatar
Tmartin113
Posts: 2
Last visit: Mon Oct 19, 2020 1:53 pm

Re: Exporting ListView data to CSV

Post by Tmartin113 »

jvierra wrote: Fri Jul 21, 2017 8:21 am You cannot directly export from a ListView. Use a DataGridView to export to a CSV.
Is there a one liner to export the DataGridView to a CSV or will it take some additional steps based on the data i am grabbing?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Exporting ListView data to CSV

Post by jvierra »

Code: Select all

$FormEvent_Load={
	$files = Get-ChildItem c:\windows -File | select name, length, fullname
    $datagridview1.DataSource = ConvertTo-DataTable $files
}

$buttonExportCsv_Click = {
	$datagridview1.SelectedRows | 
		Select-Object -expand DataBoundItem |
		Export-csv Results.csv -notype
	.\Results.csv
}
This topic is 6 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked