I have a problem to understand the way PowerShell handles properties in a pipe with multiple cmdlets. Let’s have a look at an example. I’d like to have a list of all directories located in C:. The properties I’d like to see are FullName, CreationTime and Owner. I tried it with this one-liner:
This does not work. The two properties FullName and CreationTime are not included in the Get-Acl cmdlet so these properties do not exists anymore when selected.
I know, there is a way to solve this with ForEach-Object:
That way from then on you only have to do Get-FileOwner -Path C: to get what you want. More than you asked for. Perhaps more than you need. I did take out the Select-Object Owner part because it was unnecessary, but I think you already had a fairly nice solution.
I had just woken up when I wrote that … in retrospect, I should’ve used: Get-ChildItem @PSBoundParameters so any of the advanced switches/options like -ErrorAction would follow into that cmdlet. Like: Get-FileOwner -Path C: -ErrorAction SilentlyContinue. Just a note. Hopefully one of the replies here has helped!
@Olaf: Nice way to use the calculated properties! It seems like calculated properties are more powerful than I thought. Thanks for that inspiration!
@Joshua: Yes, if I needed more than just Write-Host I would’ve probably worked with some kind of a way too complex array… I did not knew about [PsCustomObject] yet. It looks like a very interesting thing. I will have a deeper look at it!