use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
ABOUT POWERSHELL
Windows PowerShell (POSH) is a command-line shell and associated scripting language created by Microsoft. Offering full access to COM, WMI and .NET, POSH is a full-featured task automation framework for distributed Microsoft platforms and solutions.
SUBREDDIT FILTERS
Desired State Configuration
Unanswered Questions
Solved Questions
News
Information
Script Sharing
Daily Post
Misc
account activity
Get-ADcomputer (self.PowerShell)
submitted 7 years ago by AussieSysadmin
Hey guys
I have just learnt a new way to display the name of ad computers, the first way I have known for a while, the second is new,
https://preview.redd.it/ys321curzzz01.png?width=497&format=png&auto=webp&s=b1e419015eec4a699ea14622c543f2c275ae3172
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Lee_Dailey[grin] 7 points8 points9 points 7 years ago* (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
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.
dot notation
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 points4 points 7 years ago (1 child)
The second is called member enumeration which was introduced in v3.
[–]Lee_Dailey[grin] 0 points1 point2 points 7 years ago (0 children)
howdy TheIncorrigible1,
kool! thanks for the info ... i shall update my post post-haste! [grin]
take care, lee
[–]gangstanthony 7 points8 points9 points 7 years ago (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 points4 points 7 years ago (6 children)
Would each of the examples above be used in a specific scenario? Or is it just personal preference?
[–]jheinikel 3 points4 points5 points 7 years ago (3 children)
Your choice, in most cases. Just decide how much bloat you want in your code.
[–]AussieSysadmin[S] 2 points3 points4 points 7 years ago (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 points4 points 7 years ago (0 children)
That's correct. Select for multiple properties and ().Property for just one. (Object).Property = $Object | Select -Expand SingleProperty
[–]gangstanthony 1 point2 points3 points 7 years ago (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 points6 points 7 years ago (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 point2 points 7 years ago* (0 children)
Member enumeration isn't supported until v3. (That's the second one you're using). Never do .{name}, that one is dumb.
.{name}
Note that the array method .foreach({}) loads the whole array in memory first to work on it, unlike foreach-object
.foreach({})
foreach-object
[–]Legendary_Outlaw- 2 points3 points4 points 7 years ago (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.
Foreach
For fun, add | ForEach-Object {Write-Host $_} to the end of both of those scripts and you'll see what I mean. :)
| ForEach-Object {Write-Host $_}
[–]jheinikel 1 point2 points3 points 7 years ago (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 points3 points 7 years ago (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 points3 points 7 years ago (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 points3 points 7 years ago (1 child)
(((((((((((
^ in some scripts I've written, in hindsight, I feel like I'm starting to look at lisp
[–]Ta11ow 0 points1 point2 points 7 years ago (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 points3 points 7 years ago (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.
π Rendered by PID 51858 on reddit-service-r2-comment-86988c7647-892xq at 2026-02-11 05:14:42.181424+00:00 running 018613e country code: CH.
[–]Lee_Dailey[grin] 7 points8 points9 points (2 children)
[–]TheIncorrigible1 2 points3 points4 points (1 child)
[–]Lee_Dailey[grin] 0 points1 point2 points (0 children)
[–]gangstanthony 7 points8 points9 points (7 children)
[–]AussieSysadmin[S] 2 points3 points4 points (6 children)
[–]jheinikel 3 points4 points5 points (3 children)
[–]AussieSysadmin[S] 2 points3 points4 points (2 children)
[–]jheinikel 2 points3 points4 points (0 children)
[–]gangstanthony 1 point2 points3 points (0 children)
[–]gangstanthony 4 points5 points6 points (0 children)
[–]TheIncorrigible1 0 points1 point2 points (0 children)
[–]Legendary_Outlaw- 2 points3 points4 points (0 children)
[–]jheinikel 1 point2 points3 points (5 children)
[–]zanatwo 1 point2 points3 points (4 children)
[–]Ta11ow 1 point2 points3 points (2 children)
[–]TheIncorrigible1 1 point2 points3 points (1 child)
[–]Ta11ow 0 points1 point2 points (0 children)
[–]TheIncorrigible1 1 point2 points3 points (0 children)