ca9163d9
ca9163d9

Reputation: 29227

Tee-Object to two pipelines?

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.

Upvotes: 2

Views: 471

Answers (3)

ca9163d9
ca9163d9

Reputation: 29227

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

Upvotes: 1

Karthick Ganesan
Karthick Ganesan

Reputation: 385

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"

Upvotes: 0

js2010
js2010

Reputation: 27606

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 }

Upvotes: 4

Related Questions