user1172456
user1172456

Reputation: 153

Powershell Compare-Object pipe to file and function

I'm a Powershell rookie and I would like to pipe the result of Compare-Object to a Write-Progress function (progress bar) as well as a .csv file. I have the following, but it only writes to the file. Can I only use a single pipe? I have large directories to compare and really don't want to loop over the results again after using Compare-Object:

$Counter = 0
Write-Host "Comparing Folders"
compare-object $dirList1 $dirList2 | %{
Write-Progress -Activity "Comparing Folders" -Status "$_" -PercentComplete ($counter/$TotalFolderCount * 100)
sleep -milliseconds 20
$Counter++
} | ft inputobject, @{n="Only In Directory";e={ if ($_.SideIndicator -eq '=>') { "$dir2" }  else { "$dir1" } }} | Out-File $outFile

Upvotes: 1

Views: 949

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174485

You can replace Out-File with Tee-Object

Tee-Object will write the input objects to a file and pass them along the pipeline, e.g.:

"Test01","Test02" | Tee-Object -FilePath C:\temp\tee.txt | % {
    Write-Host $_ -ForegroundColor Green
}

You'll see that the strings are written to the screen in bright green and if you inspect the file, you'll see it contains the strings as well:

PS C:\> Get-Content C:\temp\tee.txt
Test01
Test02

Upvotes: 1

Related Questions