• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

Export Group in AD with Users in it

Hi,
I want an excel exported from AD for Groups & their respective members.

I don't want to use QAD & the format has to be that One Group per row & all the members in it in the same row but in second column.

Only SamAccountName of user should be there.

Server & Domain is 2008R2

Regards,
A
0
Ackles
Asked:
Ackles
  • 7
  • 6
1 Solution
 
SubsunCommented:
You can try this code and see if you get the result in expected format..
Get-ADGroup -Filter * | % {
$members = Get-ADGroupMember $_.DistinguishedName -Recursive | 
	?{$_.objectClass -eq "user"} | 
	Select -ExpandProperty SamAccountName
$_ | Select @{N="GroupName";E={$_.Name}},@{N="members";E={$members -join "`n"}}
}| Export-Csv C:\report.csv -nti

Open in new window


Here is a slightly modified code (Different output format) which will help you to filter through members..
Get-ADGroup -Filter * | % {
$Group = $_.Name
Get-ADGroupMember $_.DistinguishedName -Recursive | 
	?{$_.objectClass -eq "user"} | 
	Select @{N="GroupName";E={$Group}},SamAccountName
}| Export-Csv C:\report.csv -nti

Open in new window

0
 
AcklesAuthor Commented:
I know this, please read the question.
I want one row per Group & all the users in the next column but in same row.
0
 
SubsunCommented:
Thats what the first code does.. Did you try it?
If not provide a sample of how it should be, so I can modify the code for you..
0
What Security Threats Are We Predicting for 2018?

Cryptocurrency, IoT botnets, MFA, and more! Hackers are already planning their next big attacks for 2018. Learn what you might face, and how to defend against it with our 2018 security predictions.

 
AcklesAuthor Commented:
Column A, Row 1 = Group1
Column B, Row 1= Allusers in Group1

Column A, Row2=Group2
Column B, Row2= Allusers in Group2
0
 
SubsunCommented:
First code should do it.. The output will look like..Example..
0
 
AcklesAuthor Commented:
The first code only gives one user per group.
Second code just makes new row for every user.
0
 
SubsunCommented:
Did you try to expand the row as i shown in the screenshot?
0
 
AcklesAuthor Commented:
My bad, you are right. It does work.

However, would it be too much to ask if they can be separated by comma?
It's really not readable if you have 100 users in one group & so on....

Please!

A
0
 
SubsunCommented:
Yes it can be done... in line 5 change -join "`n" to -join ","
0
 
AcklesAuthor Commented:
Get-ADGroup -Filter * | % {
$members = Get-ADGroupMember $_.DistinguishedName -Recursive |
      ?{$_.objectClass -eq "user"} |
      Select -ExpandProperty SamAccountName
$_ | Select @{N="GroupName";E={$_.Name}},@{N="members";E={$members -join ","}}
}| Export-Csv C:\report.csv -nti


Like this?
0
 
SubsunCommented:
yes..
0
 
AcklesAuthor Commented:
Great Thanks a bunch!!!
A
0
 
AcklesAuthor Commented:
Perfect!!!
0

Featured Post

A Cyber Security RX to Protect Your Organization

Join us on December 13th for a webinar to learn how medical providers can defend against malware with a cyber security "Rx" that supports a healthy technology adoption plan for every healthcare organization.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now