0

I need to get specific users from specific Groups in Active Directory.

So far I have this:

$Groupnames = get-adgroup -Filter "name -like '$Groupfilter'" -Properties * -SearchBase $Grouppath | Select-Object Name, @{ Name='Username'; Expression={ Get-ADGroupMember -identity $($_.Name) -Recursive | Get-ADUser -Property SamAccountName | Select -ExpandProperty SamAccountName } } 

This works to get the Groups with their names. Now I want to get all users from these groups. what works but the formating is completly off. I want this:

Name Username ---- -------- Group1 user1adm Group2 {user1adm, user1, user2, user2adm...} Group3 {user1adm, user3, user2adm, user6...} 

But I get this:

{user1adm, user1, user2, user2adm...} 

With that formatting I can't see all users.

My goal at the end is also to exclude users who end with adm, but I don't know how to do that.

Can you help me?

6
  • Change Select Name to Select -Expand Name Commented Feb 19, 2021 at 10:21
  • Yeah it helped in a way but it's still like this: {user1, user1adm, user2, user3...} Why is there {} and ... at the end. It doesn't show the rest of the users. Commented Feb 19, 2021 at 12:24
  • That's just PowerShell way of indicating that there are multiple values in the property. What output do you expect? Commented Feb 19, 2021 at 12:34
  • I want to put them into a CSV {} are not a huge problem, I can get rid of them. But that it doesn't show all users is the bigger Problem. Commented Feb 19, 2021 at 12:42
  • Right, but what would the CSV file look like? One user per line/record? Commented Feb 19, 2021 at 12:46

1 Answer 1

1

Get-ADGroupMember can return objects of type 'user', 'group' or 'computer', so piping the returned objects straight through to Get-ADUser could get you into trouble if one of the objects is not a user.

Having said that, the objects returned from Get-ADGroupMember already contain the SamAccountName property you are after, so you can eliminate Get-ADUser from the code.

$Groupnames = Get-ADGroup -Filter "name -like '$Groupfilter'" -SearchBase $Grouppath | Select-Object Name, @{Name = 'Username'; Expression = { ($_ | Get-ADGroupMember -Recursive | Select-Object -ExpandProperty SamAccountName | Where-Object { $_ -notmatch 'adm$' } ) -join ', ' } } # output the result on screen $Groupnames | Format-Table -AutoSize # output to CSV file $Groupnames | Export-Csv -Path 'Path\To\The\GroupMembers.csv' -NoTypeInformation 
Sign up to request clarification or add additional context in comments.

6 Comments

Wow thank you so much, thats exactly what i needed👍
Oh no sorry I just found a Problem that I can not solve... When I export all in a CSV and then open that in Excel, the first "" it deletes but the "" in Usernames it doesn't how can I fix this?
@Forestbird May I ask why you un-accepted the answer?
@Forestbird Try adding -UseCulture to the Export-Csv command
And the even bigger Problem, at the end of the Usernames it just writes ... when there are to many Usernames!
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.