Tuesday, April 08, 2014

Using Select-Object with Expressions

This is such a cool feature of PowerShell, being able to look at data that might not be available directly on the objects.

There’s a couple different ways to solve this that I’ve found:

1. Create a new PSObject and add the new properties to it

2. Use Select-Object with expressions

I tend to use Select-Object when I just want to create a property before displaying it, typically for ad-hoc scripts that I plan on throwing away.  I tend to use PSObjects when I want to use the property elsewhere in a script.

Here is a simple example showing how to use Select-Object with AD Groups:

 

### Get some Groups

Get-ADGroup--LDAPFilter "(mailnickname=a*)" -Properties member |

 

### Select a property and create a new one using an expression

Select-Object -Property name,@{Name="Member Count"; Expression = {$_.member.count}}

 

 

Here is what the output looks like:

 

 

name                       Member Count

----                       ------------

Adaptive-All DG                      36

AAB DG                                0

AdsRUS Monthly DG                     4

Applicious DG                        10

Advanced-Development DG              33

 

 

No comments: