Command Script to find Distribution Lists/Groups for multiple users

Charlie Pearce 0 Reputation points
2023-01-19T11:56:41.4466667+00:00

Hi I have a spreadsheet/csv of 300+ users, with their name, SAMaccountname, primarySMTP etc.

I'm looking for a way to run a command against each of these users and list their Group/DL membership on ExchangeOnline.

I know this can be done for a single user, using something like:

Get-Recipient -Filter "Members -eq 'CN=deleted,deleted,OU=deleted.onmicrosoft.com,OU=Microsoft Exchange Hosted Organizations,DC=deleted,DC=PROD,DC=OUTLOOK,DC=COM'"

or

Get-DistributionGroup | where { (Get-DistributionGroupMember $.Name | foreach {$.PrimarySmtpAddress}) -eq "deleted"}|fl DisplayName,GroupType,OrganizationalUnit,PrimarySmtpAddress

But I'm looking for a way to batch this for all 340 users I need to run this against....

Microsoft Exchange Online
Microsoft Exchange Online Management
Microsoft Exchange Online Management
Microsoft Exchange Online: A Microsoft email and calendaring hosted service.Management: The act or process of organizing, handling, directing or controlling something.
4,578 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,553 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Andy David - MVP 149.2K Reputation points MVP
    2023-01-19T12:18:56.88+00:00

    If you already have the csv then import/set to $users and you could run something like using the AzureAD module ( or set to Get-DistributionGroupMember using Exo Powershell)

    $report = Foreach ($user in $users) {
       $groups = $user | Get-AzureADUserMembership
    
       # create output objects with username and groups:
       Foreach ($group in $groups) {
         [PSCustomObject][ordered]@{
           UserDisplayName   = $user.DisplayName
           UserPrincipalName = $user.UserPrincipalName
           GroupDisplayName  = $group.DisplayName
     }}}
    
    
    
    $report | export-csv groups.csv -NoTypeInformation
    

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  3. Limitless Technology 44,401 Reputation points
    2023-01-23T08:49:36.71+00:00

    Hi. Thank you for your question and reaching out. I’d be more than happy to help you with your query.

    You can try this script instead:

    $<VariableName> = Get-AddressList -Identity <AddressListIdentity>; Get-Recipient -ResultSize unlimited -RecipientPreviewFilter $<VariableName>.RecipientFilter | select Name,PrimarySmtpAddress,HiddenFromAddressListsEnabled

    For more information/reference, please see https://learn.microsoft.com/en-us/exchange/address-books/address-lists/manage-address-lists?source=recommendations

    If the reply was helpful, please don’t forget to upvote or accept as answer, thank you.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.