Tee-Object to two pipelines?

Viewed 196

I have the following code.

$summary = . {
    while ($true) { 
        # Generating huge list of psobject 
    }
} | 
Tee-Object -FilePath 'fname.csv' | # Need to process the input objects before writing to CSV
    Group-Object -Property xxx | Select Name,Count

However, I need to process the input objects before writing to fname.csv. Is it possible to Tee the object to two pipelines?

I tried

$summary = . {
    while ($true) { 
        # Generating huge list of psobject 
    }
} | 
For-Each {
    $_ | ConvertTo-Csv -NoTypeInformation | Out-File -Append 'file.csv'
    $_
} |
Group-Object -Property xxx | Select Name,Count

But the headers are repeated every line in file.csv.

3 Answers

I'm not sure what you want to do. Does this help? The objects from get-process get passed to two different pipelines.

get-process cmd | foreach-object { $_ | measure-object
                                   $_ | export-csv -append whatever.csv }

This should do the work in the question. It doesn't Tee to two pipelines, which may be needed for some use cases, though.

$summary = . {
    while ($true) { 
        # Generating huge list of psobject 
    }
} | 
ForEach {
    $_ | Export-Csv -NoTypeInformation -Append 'file.csv'
    $_
} |
Group-Object -Property xxx | Select Name,Count

Going after @mklement0 's guidance, does the below make it any simpler,

Get-Process excel | Tee-Object -Variable process | group processname | select name, count
$process | Export-Csv "D:\Op_GetProcessExcel.csv" -NoTypeInformation

Previous suggestion

As @AnsgarWiechers pointed out something like the following should work for you,

Get-Process | 
    group processname | 
    select name, count | 
    ConvertTo-Csv -NoTypeInformation | 
    Tee-Object "C:\Op_GetProcess.csv"
Related