4
$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'.
3

3 Answers 3

8

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"
Sign up to request clarification or add additional context in comments.

Comments

6

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:

"A","B","C"
"A1","B1","C1"
"A2","B2","C2"

You could append some rows to this CSV file using a script like this:

$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:

"A","B","C"
"A1","B1","C1"
"A2","B2","C2"
"A3","B3","C3"
"A4","B4","C4"
"A5","B5","C5"

8 Comments

It outputs numbers in the range 3 to 5. (Enter 3..5 at a PowerShell prompt to see it.) I used it in the code above merely as a way to generate more data for the CSV.
I get an error doesn't contain a method named 'op_Addition'.
Sorry, I can't reproduce. I just opened a PowerShell window, entered 3..5, and it output three numbers (3, 4, and 5).
sorry, I meant at this line of your code $rows + $addRows
@Bill_Stewart In the case that the file contains zero or one records, the result of Import-Csv is not an arry of size zero or one, but a single object. You need to declare $rows to [PsObject[]] $rows to fix the op_Addition error.
|
0

One possibility:

$CSVContent = $filesremoved | ConvertTo-Csv
$CSVContent[2..$CSVContent.count] | add-content E:\Code\powershell\logs\filesremoved.txt

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.