powershell script to export all group that the distribution list are member

hello,

i have a csv file with 100 distribution list

i need a script to export for every distribution list the active directory group that this distribution group is member

the output file can be like this:

nameof_distributionlist,member1,member2....

thanks for help
cawasakiAsked:
Who is Participating?
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.

Jeremy WeisingerSenior Network Consultant / EngineerCommented:
I'm not sure I understand what you're trying to do. Could you elaborate?
This doesn't make sense to me:
i need a script to export for every distribution list the active directory group that this distribution group is member
0
cawasakiAuthor Commented:
hello,

i need to delete 100 distribution list, and before this, i need to now in what group this ditribution list is member.
0
cawasakiAuthor Commented:
hello,

this command work for me but i need the script to do it for 100 group and get the output csv:

dsget group "CN=GROUP1,OU=Groups,DC=contoso,DC=com" -memberof
1
Hey MSSPs! What's your total cost of ownership?

WEBINAR: Managed security service providers often deploy & manage products from a variety of solution vendors. But is this really the best approach when it comes to saving time AND money? Join us on Aug. 15th to learn how you can improve your total cost of ownership today!

Jeremy WeisingerSenior Network Consultant / EngineerCommented:
$GroupList = Get-Content C:\Grouplist.txt
$ExportFile = C:\Output.csv

foreach($grp in $GroupList){
    $adgrp = Get-ADGroup $grp -Properties *
    $grp
    $adgrp.members| select @{name='GroupName';Expression={$adgrp.name}},@{name='MemberDN';Expression={$_}}
    } Export-Csv $ExportFile -NoTypeInformation

Open in new window


I think this should do it for you.
0
cawasakiAuthor Commented:
hello,

this script list the member on this group and not memberof
0
Jeremy WeisingerSenior Network Consultant / EngineerCommented:
I can't test this right now but you can try this:

$GroupList = Get-Content C:\Grouplist.txt
$ExportFile = C:\Output.csv

foreach($grp in $GroupList){
    $adgrp = Get-ADGroup $grp -Properties *
    $grp
    $adgrp.memberof| select @{name='GroupName';Expression={$adgrp.name}},@{name='MemberDN';Expression={$_}}
    } Export-Csv $ExportFile -NoTypeInformation
0
cawasakiAuthor Commented:
hello,

now its good but it not export the result on file :

[PS] D:\script\group>.\group.ps1
group1

GroupName                                                   MemberDN
---------                                                   --------
group1                                          CN=GP1,OU=Groups,OU=...
group1                                          CN=GP2,OU=Groups,OU=...
group1                                          CN=GP3,OU=Groups,OU=...

cmdlet Export-Csv at command pipeline position 1
Supply values for the following parameters:
InputObject:

Open in new window

0
cawasakiAuthor Commented:
hello

any help plz?
0
Jeremy WeisingerSenior Network Consultant / EngineerCommented:
Sorry doing this from my phone. Missed the pipe. This should do it.

$GroupList = Get-Content C:\Grouplist.txt
$ExportFile = C:\Output.csv

foreach($grp in $GroupList){
    $adgrp = Get-ADGroup $grp -Properties *
    $grp
    $adgrp.memberof| select @{name='GroupName';Expression={$adgrp.name}},@{name='MemberDN';Expression={$_}}
    } | Export-Csv $ExportFile -NoTypeInformation
0
cawasakiAuthor Commented:
hello
other error now:

[PS] D:\script\group>.\group.ps1
At D:\script\group\group.ps1:8 char:7
+     } | Export-Csv $ExportFile -NoTypeInformation
+       ~
An empty pipe element is not allowed.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : EmptyPipeElement

Open in new window

0
Jeremy WeisingerSenior Network Consultant / EngineerCommented:
Sorry no time for testing. Try this:
$GroupList = Get-Content C:\Grouplist.txt
$ExportFile = C:\Output.csv

$output = foreach($grp in $GroupList){
    $adgrp = Get-ADGroup $grp -Properties *
    $adgrp.memberof| select @{name='GroupName';Expression={$adgrp.name}},@{name='MemberDN';Expression={$_}}
    } 
    $output| Export-Csv $ExportFile -NoTypeInformation

Open in new window

0
cawasakiAuthor Commented:
its work now,
its is possible to get on output filer the group member email adress?

thanks
0
cawasakiAuthor Commented:
hello

any help plz?
0
Jeremy WeisingerSenior Network Consultant / EngineerCommented:
This will add the email address property to the output:
$GroupList = Get-Content C:\Grouplist.txt
$ExportFile = C:\Output.csv

$output = foreach($grp in $GroupList){
    $adgrp = Get-ADGroup $grp -Properties *
    $adgrp.memberof| select @{name='GroupName';Expression={$adgrp.name}},@{name='Email';Expression={$adgrp.mail},@{name='MemberOfDN';Expression={$_}}
    } 
    $output| Export-Csv $ExportFile -NoTypeInformation

Open in new window


Let me know if that's what you're looking for.
0
cawasakiAuthor Commented:
hello,

error on the script:

[PS] D:\script\group>.\group.ps1
At D:\script\group\group.ps1:4 char:38
+ $output = foreach($grp in $GroupList){
+                                      ~
Missing closing '}' in statement block.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : MissingEndCurlyBrace

Open in new window

0
Jeremy WeisingerSenior Network Consultant / EngineerCommented:
$GroupList = Get-Content C:\Grouplist.txt
$ExportFile = C:\Output.csv

$output = foreach($grp in $GroupList){
    $adgrp = Get-ADGroup $grp -Properties *
    $adgrp.memberof| select @{name='GroupName';Expression={$adgrp.name}},@{name='Email';Expression={$adgrp.mail}},@{name='MemberOfDN';Expression={$_}}
    }
    $output| Export-Csv $ExportFile -NoTypeInformation
0

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
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
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.