Solved

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

Posted on 2013-11-12
3
1,112 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 41

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

A hard and fast method for reducing Active Directory Administrators members.
This article will help to fix the below errors for MS Exchange Server 2013 I. Certificate error "name on the security certificate is invalid or does not match the name of the site" II. Out of Office not working III. Make Internal URLs and Externa…
This video discusses moving either the default database or any database to a new volume.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

707 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