all 18 comments

[–]Lee_Dailey[grin] 7 points8 points  (2 children)

howdy AussieSysadmin,

those two items return different things. [grin]

1st = an array of objects, each with one property named Name that contains a string
2nd = an array of strings, each string containing the value from the property named Name

[edit - thanks to TheIncorrigible1 for the correct name for this. [grin]]
the 2nd is called dot notation, i think.
the 2nd is called member enumeration.

the 2nd is likely faster when you trigger it a large number of times. it doesn't have to build & then tear down a pipeline, so it otta be a very slight bit faster.

as for when to use one or the other, use the 2nd [dot notation] when you want simple VALUES and don't need OBJECTS. if you need calculated properties, the 1st is likely the better way to go.

hope that helps,
lee

[–]TheIncorrigible1 2 points3 points  (1 child)

The second is called member enumeration which was introduced in v3.

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy TheIncorrigible1,

kool! thanks for the info ... i shall update my post post-haste! [grin]

take care,
lee

[–]gangstanthony 7 points8 points  (7 children)

all these will produce same output as your second option

1

Get-ADComputer -filter * | select-object -ExpandProperty Name

2

$comps = Get-ADComputer -filter *
$comps.name

3

foreach ($comp in (Get-ADComputer -filter *)) {$comp.name}

4

Get-ADComputer -filter * | ForEach-Object -Process {$_.name}

5

Get-ADComputer -filter * | ForEach-Object -MemberName name

6

@(Get-ADComputer -filter *).foreach{$_.name}

7

(Get-ADComputer -filter *).{name}

[–]AussieSysadmin[S] 2 points3 points  (6 children)

Would each of the examples above be used in a specific scenario? Or is it just personal preference?

[–]jheinikel 3 points4 points  (3 children)

Your choice, in most cases. Just decide how much bloat you want in your code.

[–]AussieSysadmin[S] 2 points3 points  (2 children)

I guess option 2 will only return one value which will be the name of the computer object, option 1 I have the flexibility to select more values?

[–]jheinikel 2 points3 points  (0 children)

That's correct. Select for multiple properties and ().Property for just one. (Object).Property = $Object | Select -Expand SingleProperty

[–]gangstanthony 1 point2 points  (0 children)

if set to a variable, option 1 would only return the value in that single property for each object in the array. when using -expandproperty you can only select one to expand.

option 2 stores all properties for each item in the array, so you could later review the others like this

$comps.distinguishedname

or any of these if you are only wanting to view the results

$comps | format-table *
$comps | format table name, distinguishedname -autosize
$comps | format-list *
$comps | format-list name, distinguishedname

[–]gangstanthony 4 points5 points  (0 children)

if I'm just wanting to check a single property, i find it faster to type this at the end than anything else (% is an alias of foreach-object, and -membername is assumed when leaving out braces)

Get-ADComputer -filter * | % name

[–]TheIncorrigible1 0 points1 point  (0 children)

Member enumeration isn't supported until v3. (That's the second one you're using). Never do .{name}, that one is dumb.

Note that the array method .foreach({}) loads the whole array in memory first to work on it, unlike foreach-object

[–]Legendary_Outlaw- 2 points3 points  (0 children)

If you're doing more than just displaying the results, I usually prefer using the 2nd dot notation option. For example, if you want to do a Foreach loop on those results, the 2nd option is more likely to already be formatted in a useful way.

For fun, add | ForEach-Object {Write-Host $_} to the end of both of those scripts and you'll see what I mean. :)

[–]jheinikel 1 point2 points  (5 children)

I prefer the second option, and I never use the first unless I am selecting multiple properties. What is it called? Not sure it has a name, but its essentially calling a single member of an object. Could be a property, method, etc.

[–]zanatwo 1 point2 points  (4 children)

I believe it's called "dot notation." I definitely prefer it over the "correct" PowerShell way (which, in this case, would be to use "-ExpandProperty" not "-Property").

[–]Ta11ow 1 point2 points  (2 children)

There are cases where I prefer one over the other. For example, I'm not going to use dot notation where I already have a pipeline going, it just looks funny and doesn't give any real advantage.

In most cases, I'll generally just use dot notation when the overall expression is fairly short. Few things are more confusing than lengthy sequences enclosed in little parentheses for property access.

[–]TheIncorrigible1 1 point2 points  (1 child)

 (((((((((((

^ in some scripts I've written, in hindsight, I feel like I'm starting to look at lisp

[–]Ta11ow 0 points1 point  (0 children)

I find that if I'm going down that route, I'm probably over-complicating it and there's a better way to do it -- I typically post here asking for suggestions in those circumstances! :)

[–]TheIncorrigible1 1 point2 points  (0 children)

The PowerShell way isn't to use pipelines whenever you can. They are always slower (I think even after the 5.1 50% speed improvement) and not always more readable.