Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I append files using export-csv for PowerShell 2?

$filesremoved | export-csv -Path E:\Code\powershell\logs\filesremoved.txt -NoTypeInformation

I've also tried

$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

Export-Csv : A parameter cannot be found that matches parameter name 'Append'.
like image 235
software is fun Avatar asked Dec 09 '22 10:12

software is fun


1 Answers

I have no idea what $filesremoved include, but to append CSV-output in PS2.0, you could try somthing like this:

$filesremoved | ConvertTo-Csv -NoTypeInformation | Select-Object -Skip 1 | Out-File -Append -FilePath "test2.csv"

Select-Object -Skip 1 is used to remove the header. You should however specify the columns-order you need, the delimiter and maybe encoding, like:

$filesremoved | Select-Object -Property Name, Date | ConvertTo-Csv -Delimiter ";"  -NoTypeInformation | Select-Object -Skip 1 | Out-File -Append -Encoding ascii -FilePath "test2.csv"
like image 166
Frode F. Avatar answered Jan 13 '23 12:01

Frode F.