Tuesday, 25 October 2016

Exporting DLs with Last modify date, Date created, DL members, Members display name etc. Details in Office365 server using PowerShell.

Or, PowerShell script to export DLs in Office365 server containing details like Last Modify, Date Created, DL Members, Members Display Name, Owners etc...

Descriptions: Sometime you may need to export the DLs with their last modify date, members list and creation date for audit or inventory purpose.  You can use below PowerShell command to get these details exported in an .CSV file.

Note: Please understand the risk before executing any commands/scripts.

Steps:
Login to Office365 Server Admin PowerShell Console with administrative rights.

Copy all script section contents and run that on powershell to get the DLs details as explained above. You can change the yellow marked path and file name if you want results to be exported to some other path and with some other file name as per your requirements.

-----------------------------------------------------------------------------------------------------------------------
#Prepare Output file with headers
Out-File -FilePath d:\new.csv -InputObject "Distribution Group DisplayName,Distribution Group Email,When Created,Last Modified,Owner,Member DisplayName, Member Email, Member Type" -Encoding UTF8


#Get all Distribution Groups from Office 365
$objDistributionGroups = Get-DistributionGroup -ResultSize Unlimited

#Iterate through all groups, one at a time    
Foreach ($objDistributionGroup in $objDistributionGroups)
{    
               
                write-host "Processing $($objDistributionGroup.DisplayName)..."

                #Get members of this group
                $objDGMembers = Get-DistributionGroupMember -Identity $($objDistributionGroup.PrimarySmtpAddress)
                
                write-host "Found $($objDGMembers.Count) members..."
                
                #Iterate through each member
                Foreach ($objMember in $objDGMembers)
                {
                                Out-File -FilePath d:\new.csv -InputObject "$($objDistributionGroup.DisplayName),$($objDistributionGroup.PrimarySMTPAddress),$($objDistributionGroup.WhenCreated),$($objDistributionGroup.WhenChanged),$($objDistributionGroup.Managedby),$($objMember.DisplayName),$($objMember.PrimarySMTPAddress),$($objMember.RecipientType)" -Encoding UTF8 -append
                                write-host "`t$($objDistributionGroup.DisplayName),$($objDistributionGroup.PrimarySMTPAddress),$($objDistributionGroup.WhenCreated),$($objDistributionGroup.WhenChanged),$($objDistributionGroup.Managedby),$($objMember.DisplayName),$($objMember.PrimarySMTPAddress),$($objMember.RecipientType)"
                }
}
-----------------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment