In Power Shell I try to get all members of an AD group with the following command:
$users=Get-ADGroupMember -Identity 'AD-Group-XYZ' -Recursive | Select SamAccountName
Then if I try to print the first username by typing:
$users[0]
I get the following output:
SamAccountName
--------------
usenameX
The question is how can I get only the output:
usenameX
and skip:
SamAccountName
--------------
I tried the following but nothing works:
$users[0].ToString()
$users[0].text
$users[0].text.ToString()
The reason is that I want to iterate in all usernames and create a string where I concatenate them and split them with a space.
Select-Object -ExpandPropertyinstead of justSelect-Object.$users[0].sAMAccountName.ForEach-Object { $_.SamAccountName }.$users | % SamAccountName(which accomplishes the same thing as Bill's suggestion, it's just faster to type and harder to understand).