Link to home
Start Free TrialLog in
Avatar of cawasaki
cawasaki

asked on

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
Avatar of Jeremy Weisinger
Jeremy Weisinger

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
Avatar of cawasaki

ASKER

hello,

i need to delete 100 distribution list, and before this, i need to now in what group this ditribution list is member.
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
$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.
hello,

this script list the member on this group and not memberof
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
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

hello

any help plz?
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
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

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

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

thanks
hello

any help plz?
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.
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

ASKER CERTIFIED SOLUTION
Avatar of Jeremy Weisinger
Jeremy Weisinger

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial