Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

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

Posted on 2013-11-12
3
Medium Priority
?
1,127 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 43

Accepted Solution

by:
Vasil Michev (MVP) earned 2000 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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

This article lists the top 5 free OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their exchange server is no longer available or some other critical issue with exchange server or impor…
Are you looking for the options available for exporting EDB files to PST? You may be confused as they are different in different Exchange versions. Here, I will discuss some options available.
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Suggested Courses

647 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