Export Group Membership, with the group name in out put

I am using dsget to export the group membership from AD groups.  When only 1 group membership is needed the script works well.  Now I need to export the group membership from 15 groups.  I can easily get the group membership into a single csv file.  Here is what is missing from the exported info.

The group name.

After exporting the 15 groups memebers, I have a list of user names, without a group name.  What would be ideal is to some how have the group name and the user name in the same record.  

Output Now
<List of AD Group 1>
"CN=First Name,OU=Users OU,OU=Upper OU,DC=Domain,DC=local"
"CN=2nd Name,OU=Users OU,OU=Domain Users,DC=Domain,DC=local"

<List of AD Group 2>
"CN=3rd Name,OU=Users OU,OU=Domain Users,DC=Domain,DC=local"
"CN=4th Name,OU=Users OU,OU=Domain Users,DC=Domain,DC=local"

Desired Output
"ADGroup1","CN=First Name,OU=Users OU,OU=Upper OU,DC=Domain,DC=local"
"ADGroup1","CN=2nd Name,OU=Users OU,OU=Domain Users,DC=Domain,DC=local"

"ADGroup2","CN=3rd Name,OU=Users OU,OU=Domain Users,DC=Domain,DC=local"
"ADGroup2","CN=4th Name,OU=Users OU,OU=Domain Users,DC=Domain,DC=local"

I am using a windows batch file with a for loop to export all 15 group memberships.  
FOR /F "eol=; tokens=1,2 delims=#" %%i in (GroupList.txt) do (
    DSGet Group "%%i" -members -expand
I am open to suggestings for using other programs to do this export.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi, I'm no good at batch, but this Powershell script should do the job for you as well.



Get-Content GroupList.txt | ForEach {
    $GroupName = $_
    Get-ADGroup -Filter {Name -eq $GroupName} | Get-ADGroupMember | Select @{n='Group Name';e={$GroupName}},DistinguishedName, Name
} | Export-CSV GroupMembers.csv -NoTypeInformation

Open in new window

epmmisAuthor Commented:
Thanks.  I am a power shell novice.
Can the power shell script be configured to loop throught a list of group names from a text file?.
There are 15 groups to process.  
I would to output the group list into 1 file.
Yes, in the code above, GroupList.txt is the input file, and GroupMembers.csv is the single output file.

Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

epmmisAuthor Commented:
I put this group into the grouplist.txt file.
"CN=MyGroup,OU=My App,OU=Windows,OU=Top,DC=domain,DC=local"

Here was the result.
The term 'Get-ADGroup' is not recognized as the name of a cmdlet, function, scr
ipt file, or operable program. Check the spelling of the name, or if a path was
 included, verify that the path is correct and try again.
At C:\ListExport.ps1:3 char:16
+     Get-ADGroup <<<<  -Filter {Name -eq $GroupName} | Get-ADGroupMember | Sel
ect @{n='Group Name';e={$GroupName}},DistinguishedName, Name
    + CategoryInfo          : ObjectNotFound: (Get-ADGroup:String) [], Command
    + FullyQualifiedErrorId : CommandNotFoundException
In GroupList.txt, it is searching by Name, so you only need the group name in the file. If you want to search for the full DN, change
{Name -eq $GroupName}
{DistinguishedName -eq $GroupName}

First though, you need the ActiveDirectory module. You can either use PSRemoting to get the module from a DC

And then in the script, change Get-ADGroup and Get-ADGroupMember to Get-RemADGroup and Get-RemADGroupMember where Rem is the Prefix you set when importing the module.

Or, you can install RSAT onto your machine.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
epmmisAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Active Directory

From novice to tech pro — start learning today.