ExpandProperty rocks - sometimes

Select-Object will select the object properties that you want to see. So it removes all properties you did not specify, but it always returns an object:

Get-Process | Select-Object Name

Often, you are not interested in an object but prefer the content of an object property. When you specify -ExpandProperty, Select-Object will actually take an object and replace the entire object with the content of one of its properties:

Get-Process | Select-Object -ExpandProperty Name

This is extremely useful and roughly the equivalent of this:

Get-Process | Foreach-Object { $_.Name }

However, you should note that -ExpandProperty has a bug in v2 that can occur with certain object types. So if the result returned by -ExpandProperty raises strange exceptions when you process it, you should use the Foreach-Object approach, which always works right.

Twitter This Tip! ReTweet this Tip!


Posted May 11 2010, 08:00 AM by ps1

Comments

jbruns wrote re: ExpandProperty rocks - sometimes
on 05-11-2010 9:35 AM

You say "you should note that -ExpandProperty has a bug in v2".

The next logical question is, has it been reported to MS and/or do they already know about this?

Twitter Trackbacks for ExpandProperty rocks - sometimes - Power Tips - PowerShell.com [powershell.com] on Topsy.com wrote Twitter Trackbacks for ExpandProperty rocks - sometimes - Power Tips - PowerShell.com [powershell.com] on Topsy.com
on 05-11-2010 10:52 AM

Pingback from  Twitter Trackbacks for                 ExpandProperty rocks - sometimes - Power Tips - PowerShell.com         [powershell.com]        on Topsy.com

Episode 112 – Scripting Games 2010 Roundup with Ed Wilson and Joel Bennett « PowerScripting Podcast wrote Episode 112 – Scripting Games 2010 Roundup with Ed Wilson and Joel Bennett « PowerScripting Podcast
on 05-17-2010 5:35 AM

Pingback from  Episode 112 – Scripting Games 2010 Roundup with Ed Wilson and Joel Bennett «  PowerScripting Podcast

excel formulas wrote re: ExpandProperty rocks - sometimes
on 05-28-2011 8:35 PM

I have been wondering what I was doing wrong, but it seems that I am using v2, so I think it may just be the bug in the system that's causing my headaches.  This page, and this entire site for that matter, are extremely useful.  Learning PowerShell reminds me of when I first learned Excel formulas.  It was really hard at first, but once I "got it" I felt I could do anything within the program.  You all should make up a PowerShell for Dummies book!  lol  It would sure be useful to people like me.

Bytes land wrote re: ExpandProperty rocks - sometimes
on 06-08-2011 7:10 AM

When I try that final command, I get this error:

Select-Object : Cannot expand property "name" because it has nothing to expand. At line:1 char:16

+ gps *ss |select  <<<< -ExpandProperty name

What could this mean and what should I do? Thank you in advance.

Jakpoderwacdziewczyne wrote re: ExpandProperty rocks - sometimes
on 10-29-2011 4:18 PM

Powershell is not easy to learn... have been struggeling with it for at least a week with noresults for the first couple of days. Finally I'm starting to understand it little by little..

Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.