I am writing a script where I would like to spit out a bunch of records and then display the count as the last line. This is what I have so far:
Get-Whatever -Department $Deparment
Write-Host (Get-Whatever -Department $Deparment).Count " records found"
But i’m curious if there is a way to do it without executing it twice. I thought that I had read you could use $$ somewhere but this isn’t working. Is there a better way to do this, or do I just have to run it twice?
My Desired output would look something like this:
Name
-------
Abe
Joe
Bill
3 records found
Why don’t you simply assign the result to a variable?
Note the
@(..). It ensures that even whenGet-Whateverdoesn’t return anything,$dwill be empty array.Other way is e.g.
Tee-Object. However, it somewhat “magically” creates variables, so it is not as readable as the first approach:As for
Tee-Object(from documentation, tryhelp tee-object -online):