How to use dsquery to list the members of a distribution list?

892 Views Asked by At

I have this command to find a distribution list object

dsquery * -filter "(&(cn=*group))"

but now how can I find the users from that, I want to loop through and get their names and email addresses from it.

Thanks

1

There are 1 best solutions below

0
On

Now that you have the group name, you can use PowerShell to iterate through the group and extract the information you need:

Import-Module ActiveDirectory
$group = read-host "Please Enter Group Name: "

$members = Get-ADGroupMember -Identity $group -Recursive
    ForEach ($member in $members) {
        $memberType = $member.objectClass
           If ($memberType -eq 'user') {
           Get-ADUser -Filter "name -like '*$member'" -Properties cn,mail | Out-File c:\temp\Group_Members.csv -append
            }
        }

The code above will prompt for the group name and export the list of members, including where there is a nested group into the a file called Group_Members.csv in c:\temp.

You will need to ensure that:

  • Script execution is enabled in Powershell;
  • That RSAT is installed on the device that the script is executed from;
  • That the script is executed with administrator privileges.