Revisiting Old Posts: Get-Process | Dir (about ValueFromPipelineByPropertyName)

PowerShell Team

A number of PowerShell MVPs and PowerShell team members are helping me to compile a table of contents for our blog, which basically means that I’m getting to go back and revisit all of the fun stuff posted to the PowerShell blog over the past few years.  It’s a fun an educational process, and, hopefully, when we’re done the blog will be a much simpler place to explore.

I’m trying to keep my eye on the ball, and compile the table of contents instead of expounding more on previous posts, but some things are just too cool to resist.

For instance, check out this post from September 17th, 2006:

Get-Process | Dir

The post talks some about how you can actually pass many interesting things into Get-Item you didn’t know you could pass, but it doesn’t indicate how this bit of PowerShell magic works.

In a cmdlet or an advanced function, it’s possible to mark something as ValueFromPipeline (which will take an exact object for a pipeline), but it’s also possible to take an item from the pipeline by a property name.  Since Get-Process has a Path property, and the Path parameter is marked  ValueFromPipelineByPropertyName, it uses the Path property of each process object as the path parameter of each Get-ChildItem.

Another thing you can do with this knowledge is get all of the different directories that processes are running in.  Check it out:

Get-Process |
    Get-Item -ErrorAction SilentlyContinue |
    Group-Object Directory |
    Sort-Object Count -Descending

PowerShell is Wonderful.

Hope this Helps,

James Brundage [MSFT]

0 comments

Discussion is closed.

Feedback usabilla icon