1

I'm trying to export the contents of folders in a directory to a CSV. The issue I'm running into is that I can't figure out how to get the export to 2 different paths.

Simply adding another Export-Csv just adds a empty Csv to the 2nd path.

Get-ChildItem -Path *root_path* -Filter *.zip -Recurse | 
Select-Object BaseName, CreationTime |

Export-Csv -Path "*path1*_$((Get-Date).ToString(' yyyy-MM-dd HHmmss')).csv"

Export-Csv -Path *path2*_$((Get-Date).ToString(' yyyy-MM-dd HHmmss')).csv
duckfeet23
  • 54
  • 8

3 Answers3

0

It's because econd export is not part of the pipeline. You could store the results first and then export them.

$dat = Get-ChildItem -Path *root_path* -Filter *.zip -Recurse | Select-Object BaseName, CreationTime 
'*path1*','*path2*' | foreach {Export-Csv -InputObject $dat -Path "$_$((Get-Date).ToString('yyyy-MM-dd HHmmss')).csv"}
AdamL
  • 12,421
  • 5
  • 50
  • 74
  • So your script does output 2 csv but, its not outputting the details collected by the "Select-Object". I tried storing the "Select-Object" with no luck. – duckfeet23 Jul 28 '20 at 18:18
  • @duckfeet23 The part with select-object you got right in your question. Anyway I added it, try now. – AdamL Jul 28 '20 at 19:21
  • It's giving a me a error basically stating the paths can't be in the middle of the pipe. – duckfeet23 Jul 28 '20 at 20:37
  • @duckfeet23 Because they can't, I made a mistake. Remove extra |, it should be 2 separate pipelines. Also, don't create paths like that, join-path is safer. – AdamL Jul 29 '20 at 07:28
0

You could try Tee-Object.

One-liner:

Get-ChildItem -Path *root_path* -Filter *.zip -Recurse | Select-Object BaseName, CreationTime |ConvertTo-Csv -NoTypeInformation | Tee-Object -FilePath "*path1*_$((Get-Date).ToString(' yyyy-MM-dd HHmmss')).csv" -Append | Out-File "*path2*_$((Get-Date).ToString(' yyyy-MM-dd HHmmss')).csv"

See Example 3 of the Tee-Object docu.

Moerwald
  • 10,448
  • 9
  • 43
  • 83
0

What you are saying is that you want to export the same thing to two different files?

$data = Get-ChildItem -Path *root_path* -Filter *.zip -Recurse 
 
$data | Select-Object BaseName, CreationTime | Export-Csv -Path "*path1*_$((Get-Date).ToString(' yyyy-MM-dd HHmmss')).csv"
$data | Select-Object BaseName, CreationTime | Export-Csv -Path "*path2*_$((Get-Date).ToString(' yyyy-MM-dd HHmmss')).csv"
Daniel Björk
  • 2,475
  • 1
  • 19
  • 26