Powershell - Export-CSV and Append

I have a script, for example:

$in_file = "C:\Data\Need-Info.csv" $out_file = "C:\Data\Need-Info_Updated.csv" $list = Import-Csv $in_file ForEach ( $user in $list ) { $zID = $user.zID ForEach-Object { Get-QADUser -Service 'domain.local' -SearchRoot 'OU=Users,DC=domain,DC=local' -SizeLimit 75000 -LdapFilter "(&(objectCategory=person)(objectClass=user)(PersonzID=$zID))" | Select-Object DisplayName,samAccountName,@{Name="zID";expression={$zID}} | Export-Csv $out_file -NoTypeInformation -Force } } 

However, I cannot get it to output all the results to the $ out_file file, as it does not seem to add data to the csv file. I have tried several things, ideas and suggestions found on the Internet. None of them seem to work, and I feel like I'm missing something quite simple here. Is there any way to do this adding data to a file? If so, can you give an example or piece of code to make it work this way? I do not ask you to rewrite all the code or something like that, just a way to add exported data to the file that is being output.

+6
source share
4 answers

Convert the foreach loop to a foreach object and move export-csv outside the outter foreach object so that you can pass all the objects to export-csv.

Something like this (untested):

 $list | ForEach { $zID = $_.zID ForEach-Object { Get-QADUser -Service 'domain.local' -SearchRoot 'OU=Users,DC=domain,DC=local' -SizeLimit 75000 -LdapFilter "(&(objectCategory=person)(objectClass=user)(PersonzID=$zID))" | Select-Object DisplayName,samAccountName,@{Name="zID";expression={$zID}} | } } |Export-Csv $out_file -NoTypeInformation -Force 
+9
source

-append is broken in Powershell v2.0 Use the work of Dmitry Sotikov: http://dmitrysotnikov.wordpress.com/2010/01/19/export-csv-append/

I would recommend a great solution for manojlds!

+4
source

As mentioned in Sune, PowerShell v3 Export-Csv has an Append flag, but lack of character encoding protection . manojlds is correct as your code writes all new data to a new CSV file.

Meanwhile, you can add data to the CSV:

  • Convert objects to CSV using ConvertTo-Csv
  • Separate the header and enter information if necessary, and collect CSV data only
  • Add new CSV data to CSV file using Add-Content or Out-File, be sure to use the same character encoding

Here is an example:

 1..3 | ForEach-Object { New-Object PSObject -Property @{Number = $_; Cubed = $_ * $_ * $_} } | Export-Csv -Path .\NumTest.csv -NoTypeInformation -Encoding UTF8 # create new data $newData = 4..5 | ForEach-Object { New-Object PSObject -Property @{Number = $_; Cubed = $_ * $_ * $_} } | ConvertTo-Csv -NoTypeInformation # strip header (1st element) by assigning it to Null and collect new data $null, $justData = $newData # append just the new data Add-Content -Path .\NumTest.csv -Value $justData -Encoding UTF8 # create more new data, strip header and collect just data $null, $data = 6..9 | ForEach-Object { New-Object PSObject -Property @{Number = $_; Cubed = $_ * $_ * $_} } | ConvertTo-Csv -NoTypeInformation # append the new data Add-Content -Path .\NumTest.csv -Value $data -Encoding UTF8 # verify Import-Csv .\NumTest.csv # clean up Remove-Item .\NumTest.csv 
+3
source

After version 3 you can use:

 Export-Csv $out_file -append -notypeinformation -encoding "unicode" 
0
source

Source: https://habr.com/ru/post/908106/


All Articles