Solved

need a script to export out all Distro groups and members from office 365

Posted on 2013-11-12
3
1,098 Views
Last Modified: 2013-11-14
can someone provide a script that will output all Distro groups and members from O365 in this format:

Member                                 Group
User1                                      distro group 1
User2                                      distro group 1
User3                                      distro group 1
User1                                      distro group 2
User3                                      distro group 2
User5                                      distro group 2
User1                                      distro group 3
User6                                      distro group 3
....
0
Comment
Question by:siber1
  • 2
3 Comments
 
LVL 38

Accepted Solution

by:
Vasil Michev (MVP) earned 500 total points
ID: 39643193
Here you go:

$list = Get-DistributionGroup -ResultSize Unlimited
$arrMsolUserData = @()

foreach ($DGName in $list) {
	$objProperties = New-Object PSObject

	$users = Get-DistributionGroupMember -Identity $DGName.PrimarySmtpAddress

   	Add-Member -InputObject $objProperties -MemberType NoteProperty -Name "PrimarySmtpAddress" -Value $DGName.PrimarySmtpAddress

   	$arrUsers = @()
    if ($users -ne $null) {
        foreach ($user in $users) {
            $arrUsers = ([string]::Join($(get-user $user.SamAccountName).UserPrincipalName,",",$arrUsers))
            }
        Add-Member -InputObject $objProperties -MemberType NoteProperty -Name "Members" -Value $arrUsers.TrimStart(",")
        }
    else {Add-Member -InputObject $objProperties -MemberType NoteProperty -Name "Members" -Value ""}
 	
	$arrMsolUserData += $objProperties
}

$arrMsolUserData | Export-Csv -Path "C:\DGMembers.csv" -NoTypeInformation

Open in new window



It will return empty groups as well. Might be a bit slow as I use UserPrincipalName as the identifier for each member, and it's calling get-user to get it. This is done because you can add non mail-enabled user to a DG as well. If you are only interested in returning mail enabled ones, it can run much faster, just replace

$(get-user $user.SamAccountName).UserPrincipalName 

Open in new window

with
$user.PrimarySmtpAddress

Open in new window

0
 

Author Comment

by:siber1
ID: 39643288
thx  vasilcho I will test this tonight.
0
 

Author Closing Comment

by:siber1
ID: 39644915
I just tried this again while connected via powershell to our tenant account [o365] and i'm getting all kinds of errors:

Cannot find an overload for "Join" and the argument count: "3".
At C:\Users\run.ps1:14 char:40
+             $arrUsers = ([string]::Join <<<< ($(get-user $user.SamAccountName).UserPrincipalName,",",$arrUsers))
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest

Cannot find an overload for "Join" and the argument count: "3".
At C:\Users\Desktop\run.ps1:14 char:40
+             $arrUsers = ([string]::Join <<<< ($(get-user $user.SamAccountName).UserPrincipalName,",",$arrUsers))
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest

Method invocation failed because [System.Object[]] doesn't contain a method named 'TrimStart'.
At C:\Users\Desktop\run.ps1:16 char:115
+         Add-Member -InputObject $objProperties -MemberType NoteProperty -Name "Members" -Value $arrUsers.TrimStart <<
<< (",")
    + CategoryInfo          : InvalidOperation: (TrimStart:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

Cannot find an overload for "Join" and the argument count: "3".
At C:\Users\run.ps1:14 char:40
+             $arrUsers = ([string]::Join <<<< ($(get-user $user.SamAccountName).UserPrincipalName,",",$arrUsers))
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest

Cannot find an overload for "Join" and the argument count: "3".
At C:\Users\run.ps1:14 char:40
+             $arrUsers = ([string]::Join <<<< ($(get-user $user.SamAccountName).UserPrincipalName,",",$arrUsers))
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest

Method invocation failed because [System.Object[]] doesn't contain a method named 'TrimStart'.
At C:\Users\run.ps1:16 char:115
+         Add-Member -InputObject $objProperties -MemberType NoteProperty -Name "Members" -Value $arrUsers.TrimStart <<
<< (",")
    + CategoryInfo          : InvalidOperation: (TrimStart:String) [], RuntimeException
0

Join & Write a Comment

Disabling the Directory Sync Service Account in Office 365 will stop directory synchronization from working.
Follow this checklist to learn more about the 15 things you should never include in an email signature from personal quotes, animated gifs and out-of-date marketing content.
In this video we show how to create a Contact in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Recipients >> Contact ta…
To show how to generate a certificate request in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.:  First we need to log into the Exchange Admin Center. Navigate to the Servers >> Certificates‚Ķ

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now