I have a script like the following:
$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 am not able to get it to output all of the results to the $out_file since it does not seem to append the data to the csv file.
Is there a way to make this append the data to a file?
Export data to a text file by saving itGo to File > Save As. Click Browse. In the Save As dialog box, under Save as type box, choose the text file format for the worksheet; for example, click Text (Tab delimited) or CSV (Comma delimited). Note: The different formats support different feature sets.
As Sune mentioned, PowerShell v3's Export-Csv has an Append flag but no character encoding protection. manojlds is correct, since your code is writing all new data to a new CSV file.
Meanwhile, you can append data to a CSV by:
Here is a sample:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With