Solved

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

Posted on 2013-11-12
3
1,103 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 40

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

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
Find out what you should include to make the best professional email signature for your organization.
The basic steps you have just learned will be implemented in this video. The basic steps are shown to configure an Exchange DAG in a live working Exchange Server Environment and manage the same (Exchange Server 2010 Software is used in a Windows Ser…
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…

809 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