SAM2009
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=Acc ounts,DC=m ydomain,DC =com
Enabled : True
GivenName : User01
Name : User01
ObjectClass : user
ObjectGUID : c98d048c-0df5-4757-bb6f-4a 13157a7845
SamAccountName : User01
SID : S-1-5-21-2063951012-160347 0910-62363 2099-32567
Surname : User01
UserPrincipalName : User01@MyDomain.com
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=Acc
Enabled : True
GivenName : User01
Name : User01
ObjectClass : user
ObjectGUID : c98d048c-0df5-4757-bb6f-4a
SamAccountName : User01
SID : S-1-5-21-2063951012-160347
Surname : User01
UserPrincipalName : User01@MyDomain.com
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}
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}
Another similar expression would be:(Get-ADUser user01).SamAccountName
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}
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}
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?
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.
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@oDbA: Thank you ... As always, this was extremely informative.
ASKER
Thank you very much! Now it' more clear.