Link to home
Start Free TrialLog in
Avatar of SAM2009
SAM2009Flag for Canada

asked on

Difference between "Get-ADUser user01 | Select SamAccountName" and "Get-ADUser user01 | Select $_.SamAccountName"

Hi,


I'm just playing with PowerShell cmd.


What the difference between these cmd below. Both give different result, why?





Get-ADUser user01 | Select SamAccountName

SamAccountName
--------------
user01


And this:

Get-ADUser user01 | Select $_.SamAccountName

DistinguishedName : CN=User01\, User01,OU=Employees,OU=Accounts,DC=mydomain,DC=com
Enabled           : True
GivenName         : User01
Name              : User01
ObjectClass       : user
ObjectGUID        : c98d048c-0df5-4757-bb6f-4a13157a7845
SamAccountName    : User01
SID               : S-1-5-21-2063951012-1603470910-623632099-32567
Surname           : User01
UserPrincipalName : User01@MyDomain.com
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

the first one selects the samaccountname the second one selects the user object filtered on the samaccountname
The second one is the syntax used when iterating with the foreach cmdlet:
Get-ADUser user01 | foreach {$_.SamAccountName}

Open in new window

would give the same result, but is usually used when you have more than a single object, e.g.
Get-ADUser -Filter 'Name -like "User0*" ' | foreach {$_.SamAccountName}

Open in new window

Another similar expression would be:
(Get-ADUser user01).SamAccountName

Open in new window

which would give you the value without the heading.
first command asks to select ie display the property named SamAccountName only
so it displays only SamAccountName

the 2nd command asks to display current object denoted by $_ so displays all the properties stored in the object
but you are trying as $_.SamAccountName but this won't    work as you expect to display only current object's SamAccountName property
the powershell separates the Property SamAccountName and just adds the word at the end of the display
if you really want to display as you expect it then you should use
get-aduser usernameHere | % { $_.SamAccountName}
Avatar of SAM2009

ASKER

When you said: "the 2nd command asks to display current object denoted by $_ so displays all the properties stored in the object "

So if it ask to display all properties store in the object why when I add "$_.SamAccountName" it does not display just SamAccount properties?
When you use Select, it accepts names of properties. SamAccountName is a property.

When you use Select with $_ syntax which is reserved for iterations, you get the object with all it's properties instead of just one because there is no property by that name.
to display just SamAccountName you have to use first command
answer to your question is No because I have given the reason and alternative command in my earlier comments to use % in addition to $_.SamAccountName
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@oDbA: Thank you ... As always, this was extremely informative.
Avatar of SAM2009

ASKER

Thank you very much! Now it' more clear.