1

This basically works

foreach ($cprev in $CopyPreventeds) {
    Write-Host ("prevented copy $(($cprev)."Name")")
    $cprev | Select-Object Path, Name, Length, LastWrite, DestinationNewer | Export-Csv '.\prevented.csv' -NoTypeInformation
}

But only the last output is written to the csv. How could I write all contents to a new csv with an output at the same time for the user in PowerShell.

3
  • 4
    Either add the parameter -Append to your Export-Csv command or remove it from inside the loop, save the output of the loop in a variable an pipe this variable to Export-Csv Commented Aug 15, 2021 at 12:22
  • $cprev | Select-Object Path, Name, Length, LastWrite, DestinationNewer | ConvertTo-Csv | Tee-Object -FilePath '.\prevented.csv' -Append | ConvertFrom-Csv is the easiest way I can think of without saving to a variable first. Commented Aug 15, 2021 at 12:39
  • The -Append did the Trick. Kudos if you post this as an official answer. Thanks a lot ! Commented Aug 15, 2021 at 12:39

1 Answer 1

3

Maybe I'm missing something?

While I appreciate a solution has already been proposed in the comments, I have to ask, given the narrow scope of the question why are we using an obscure, albeit clever technique? And/or, repeatedly invoking Export-Csv...

The question doesn't mention sparing a variable. Moreover, There doesn't appear to be a need for the ForEach loop.

$CopyPreventeds | 
Select-Object Path, Name, Length, LastWrite, DestinationNewer | 
Export-Csv '.\prevented.csv' -NoTypeInformation

In the above $CopyPreventeds already exists and remains so, unmolested after the export. You would need only to output it again for the benefit of an interactive user. All taking advantage of PowerShell's intuitive pipeline and features.

Moreover, since the iteration variable $cprev isn't needed you are still less one variable.

Note: You don't need -Append because you are streaming into a single Export-Csv command, as opposed to repeatedly invoking it.

There are at least 2 ways (probably many more) you could conveniently output to an interactive user.

1: Echo a header, something like "The following copies were prevented:" then echo the variable $CopyPreventeds, presumably to a table.

Note: That given multiple points at which you seem only interested in a subset of properties. You may think about trimming those objects beforehand:

$CopyPreventeds = 
$CopyPreventeds | 
Select-Object Path, Name, Length, LastWrite, DestinationNewer

$CopyPreventeds | Export-Csv '.\prevented.csv' -NoTypeInformation

Write-Host "The following copies were prevented:"
$CopyPreventeds | Format-Table -AutoSize | Out-Host

Note: More than 4 Properties in a [PSCustomObject] (resulting from Select-Object) where custom formatting hasn't been defined will by default output as a list, so use Format-Table to overcome that. Out-Host is then used to prevent pipeline pollution.

2: Return to using a ForEach-Object Loop for the output between the Select-Object and the Export-Csv command.

$CopyPreventeds | 
Select-Object Path, Name, Length, LastWrite, DestinationNewer 
ForEach-Object{
    "Prevented Copy : {0}, {1}, {2}, {3}, {4}" -f $_.Path, $_.Name, $_.Length, $_.LastWrite, $_.DestinationNewer |    
    Write-Host
    $_
} | 
Export-Csv '.\prevented.csv' -NoTypeInformation

In this example, when you are done outputting to the screen (admittedly a little messy), you emit $_ from the loop, thus piping it to Export-Csv just the same.

Note: there are a number of ways to construct strings, I choose to use the -f operator here because it's a little cleaning than imbedding numerous $() sub expressions. And, of course this assume you want to prefix on every line Which I personally think is gratuitous, so I'd choose something more like #1..

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for the detailed answer.INdeed there was additional help !

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.