Forum Discussion
Tee-Object / -OutVariable into existing array (i.e., add into that array - is this doable?)
Hey, Jeremy.
The first issue where Tee-Object will let you down is that only one format will apply to both the file as well as the screen output (at least within Windows PowerShell - PowerShell has some options I won't get into here), meaning you cannot have the usual table (or list) format for the screen while having a CSV format for the file. Table (or list) format doesn't make for a useful file while CSV presentation isn't fun to read on-screen.
Next, you're accruing arrays ($yadyada plus $preExistingArray, plus others if you keep growing this approach) which doesn't scale well in larger environments. It will be far more efficient (over large data sets, at least) to work with a single pipeline object approach.
This approach lends itself to then doing what you cannot with Tee-Object, which is to use the default format for the screen while still being able to export to a file in CSV format. It also solves your other challenge (subject to not using post-processing commandlets like Sort-Object, Group-Object, etc.) around displaying objects "immediately".
It's also worth pointing out that there's nothing inherently efficient with Tee-Object from a performance perspective. It's just saving a little bit of extra coding at best.
Here's a basic example (since I'm unsure how your current scripts operate) that leverages the single pipeline object approach that sends the object both to the pipeline as well as the CSV file, which avoids the performance hits from large data sets multiplied by additional arrays.
Example
$csvFilename = "D:\Data\Temp\Forum\forum.csv";
Remove-Item -Path $csvFilename -ErrorAction:SilentlyContinue;
# Work with the current object on the pipeline and let it be released once all processing is done.
Get-EXOMailbox -Anr "test.mailbox" -Properties ExchangeGuid, PrimarySmtpAddress |
ForEach-Object {
$mailbox = $_;
$mailbox |
Get-EXOMailboxStatistics -Properties MailboxTypeDetail, TotalItemSize |
ForEach-Object {
$output = [PSCustomObject] @{
id = $mailbox.ExchangeGuid;
mail = $mailbox.PrimarySmtpAddress;
type = $_.MailboxTypeDetail;
size = $_.TotalItemSize.Value.ToBytes();
}
$output | Export-Csv -NoTypeInformation -Path $csvFilename;
$output;
}
}
Screen output
CSV file output
Cheers,
Lain