Question:
1 2 |
$filesremoved | export-csv -Path E:\Code\powershell\logs\filesremoved.txt -NoTypeInformation |
I’ve also tried
1 2 |
$filesremoved | export-csv -Path E:\Code\powershell\logs\filesremoved.txt -NoTypeInformation -NoClobber |
The file appears to be overwritten every time. Is there a way to keep adding content to the file?
I get errors
1 2 |
Export-Csv : A parameter cannot be found that matches parameter name 'Append'. |
Answer:
The -Append
parameter of Export-Csv
doesn’t exist until PowerShell 3.0.
One way to work around it in PowerShell 2.0 is to import the existing CSV, create some new rows, append the two collections, and export again. For example, suppose test.csv:
1 2 3 4 |
"A","B","C" "A1","B1","C1" "A2","B2","C2" |
You could append some rows to this CSV file using a script like this:
1 2 3 4 5 6 7 8 9 10 |
$rows = [Object[]] (Import-Csv "test.csv") $addRows = 3..5 | ForEach-Object { New-Object PSObject -Property @{ "A" = "A{0}" -f $_ "B" = "B{0}" -f $_ "C" = "C{0}" -f $_ } } $rows + $addRows | Export-Csv "test2.csv" -NoTypeInformation |
Run this script and the content of test2.csv will be:
1 2 3 4 5 6 7 |
"A","B","C" "A1","B1","C1" "A2","B2","C2" "A3","B3","C3" "A4","B4","C4" "A5","B5","C5" |