1

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.

7
  • 6
    Use Select-Object -ExpandProperty instead of just Select-Object. Commented Apr 17, 2018 at 21:01
  • 3
    You can also do $users[0].sAMAccountName. Commented Apr 17, 2018 at 21:57
  • depending on the version of PS you have, $users.samAccountName should return the entire list. Commented Apr 17, 2018 at 22:07
  • 1
    A similar effect can be achieved using ForEach-Object { $_.SamAccountName }. Commented Apr 17, 2018 at 22:12
  • 1
    Or shorthand for lit's suggestion is $users | % SamAccountName (which accomplishes the same thing as Bill's suggestion, it's just faster to type and harder to understand). Commented Apr 17, 2018 at 22:21

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.