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
Solved

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

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
This article outlines the process to identify and resolve account lockout in an Active Directory environment.
The video tutorial explains the basics of the Exchange server Database Availability groups. The components of this video include: 1. Automatic Failover 2. Failover Clustering 3. Active Manager
This video discusses moving either the default database or any database to a new volume.

790 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