Skip to content

Something I noticed in Powershell v3

So, let’s say you have an object that contains objects. A bunch of AD users whose first name is Bob, for example.

$persons = get-aduser -filter {givenname –eq "Bob"}

Now you want to display the list of objects, but only one property of those objects, like DisplayName. How would you do that?

In Powershell v3, it’s simple. You can just do this:

$persons.displayname

I’ve been using v3 for so long that I didn’t realize this is relatively new functionality. I wrote a script in ISE v3 that contained something along those lines, and tried to run it on a Server 2008 box. It never worked in that environment.

Finally, I realized that in v2, you had to type a few more characters to get this to work. You can do:

$persons | foreach-object{$_.displayname}

Or

$persons | select displayname

The difference between the two is that the first choice actually outputs a list of discrete entities (objects), and the second outputs one object containing many records. The v3 versions is more like the first v2 option; it outputs a list of strings instead of one object. In either version, if you want one object as output, you need to pipe the original object to Select or Format-Table.

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*