The current article is that third article on the five-article series, which is dedicated to…
Manage Distribution Group using PowerShell in Office 365 | view and export information about Distribution Group | Part 4#5
The current article is the fourth article of the five-article series, which is dedicated to the subject of managing Distribution Group in Office 365 and Exchange Online based environment using PowerShell.The article includes two main sections:
- Management tasks that relate to the need for viewing information about Distribution Group settings, Distribution Group members, and so on.
- Management tasks that relate to the need of exporting information about Distribution Group.
Manage Distribution Group using PowerShell in Office 365 | Article Series
- Manage Distribution Group using PowerShell in Office 365 | PowerShell command reference | Part 1#5
- Manage Distribution Group using PowerShell in Office 365 | Creating New Distribution Group and managing existing Distribution Groups | Part 2#5
- Manage Distribution Group using PowerShell in Office 365 | Adding members to existing Distribution Group | Part 3#5
- Manage Distribution Group using PowerShell in Office 365 | view and export information about Distribution Group | Part 4#5
- Manage Distribution Group using PowerShell in Office 365 | Delete Distribution Group and members | Convert Distribution Group | Part 5#5
View information about Distribution Groups
View a list of all existing Distribution Groups
To get a list of all existing Distribution Groups, we use the PowerShell command:
Display all Distribution Groups list + details
PowerShell command example:
Get-DistributionGroup
Display Distribution Group Members
To get a list of all Distribution Group members, we use the following PowerShell command:
Display Distribution Group Members
PowerShell command syntax
Get-DistributionGroupMember "<Distribution Group Name>"
PowerShell command example:
Get-DistributionGroupMember "Sales France"
In the following example, we “extend” the information that we get from the basic PowerShell command- Get-DistributionGroupMember.
In this example, we ask from PowerShell to sort the result by the “DisplayName” property, and in addition, display the following properties of the members – DisplayName, Alias, and Department
PowerShell command example:
Get-DistributionGroupMember IT | Sort -Property DisplayName | Select DisplayName, Alias, Department
Count the number of Distribution Group members
In case we need to count the number of members in a specific Distribution Group, use the following PowerShell command.
PowerShell command example:
(Get-DistributionGroupMember "IT").Count
Display list of Distribution Groups with specific Email Domain name suffix
In the following example, we want to get information about Distribution Group that their E-mail address includes a specific domain name suffix.
Display list of Distribution Groups with specific Email Domain
PowerShell command syntax:
Get-DistributionGroup | Where {$_.emailaddresses –like <"*Domain Name*">} | FT -Property Name,Alias,EmailAddresses -Autosize
In our specific example, we look for Distribution Group that their E-mail address includes the domain name – o365info.com
PowerShell command example:
Get-DistributionGroup | Where {$_.emailaddresses –like "*o365info.com*"} | FT -Property Name,Alias,EmailAddresses -Autosize
Display information about Distribution Group that was updated before or after specific date range
In some scenario, we need to get a list of Distribution Groups that was created in specific date \Time or Distribution Group that was created in a specific time range.
For example:
- Scenario 1 – We want to add a user to all NEW Distribution Groups, that was created in a specific time range – last two weeks.
- Scenario 2 – We want to add a user to all Distribution Groups, that was created in a specific time range – all the Distribution Group that was created before the last two weeks.
To be able to “know” when a specific Distribution Group was created, we can query the Distribution Group property – “WhenCreated”.
To be able to find the Distribution Group that “belong” to the specific time range such as the last two weeks or the time before the current two weeks, we need to use the PowerShell logical operators.
In our example, we create two PowerShell queries that will use the following operators – ge and le, and get the following information:
- Get a list of Distribution Groups, that was created in the last 2 weeks (during the last two weeks). For this purpose, we use the PowerShell operator – ge (Greater than or equal to).
- Get a list of Distribution Groups, that was created before the last 2 weeks. For this purpose, we use the PowerShell operator – Le (Less than or equal to).
To be able to define the “time range” of two weeks, we will use the PowerShell
cmdlets Get-Date, in the following way: (Get-Date).Adddays(-14).
Get a list of Distribution Groups, that was created in the last 2 weeks
PowerShell command example:
Get-DistributionGroup | Where {$_.WhenCreated –ge ((Get-Date).Adddays(-14))} | FT DisplayName,WhenCreated
Get a list of Distribution Groups, that was created before the last 2 weeks.
PowerShell command example:
Get-DistributionGroup | Where {$_.WhenCreated –le ((Get-Date). Adddays (-14))} | FT DisplayName,WhenCreated
Display all Distribution Groups which their owner (managed by) is user X
In this example, we want to get a list of all Distribution Groups that are managed by Specific user.
Get a list of all Distribution Groups that are managed by Adele
PowerShell command example:
Get-DistributionGroup | Where {$_.ManagedBy -like “*adele*”} | FT DisplayName,ManagedBy
Display all Distribution Groups which have a moderator
In this case, we want to get information about Distribution Groups which have a moderator.
For this purpose, we can use a “simple PowerShell command” or a more sophisticated PowerShell command.
In the following example, we ask from PowerShell to get a list of all existing Distribution Groups and present the following properties – display name + the property ModeratedBy.
The information that will be presented will include Distribution Group with a moderator and also without a moderator.
PowerShell command example:
Get-DistributionGroup | FT DisplayName,ModeratedBy
In case that we want to implement more “exact search” that will display only Distribution Group that has a moderator, we can use the following PowerShell command:
PowerShell command example:
Get-DistributionGroup | Where {$_.ModeratedBy -notlike “$null”} | FT DisplayName,ManagedBy
Display all Distribution Groups that are synchronized from On-Premise Active Directory
In this example, we want to get a list of all Distribution Groups that are considered as synchronized Distribution Group.
The term synchronized Distribution Group relates to Distribution Groups that was created in On-Premise Active Directory and synchronized with Exchange Online by using Directory synchronization server.
PowerShell command example:
Get-DistributionGroup | Where {$_.IsDirSynced -eq $true} | FT DisplayName, IsDirSynced
Display Distribution Group information about delivery management
The term “delivery management” describes the setting that relates to “allowed” senders.
The senders Divided into 2 groups:
- Internal senders (organization recipients) which describe as authenticated recipients.
- External senders (non-organization recipients) which describe as unauthenticated recipients.
In Office 365 the default setting for a new Distribution Group is “Only senders inside my organization.”
In other words, by default, external recipients are not allowed to send an E-mail message to the Exchange Online Distribution Group.
Display Distribution Groups which their delivery management allows external recipients
to send E-mail
In this example, we want to get a list of all Distribution Groups that can accept E-mail from external recipients.
The parameter that defines this option is – RequireSenderAuthenticationEnabled
In case that the value of the parameter – RequireSenderAuthenticationEnabled is – $True, the meaning is that the Distribution Group will accept E-mail from external (non-organization) recipients.
Display Distribution Groups that accept E-mail from external recipients
PowerShell command example:
Get-DistributionGroup | Where {$_.RequireSenderAuthenticationEnabled -eq $True} | FT DisplayName,RequireSenderAuthenticationEnabled
Display Distribution Groups which their delivery management doesn’t allow external recipients to send E-mail
In this example, we want to get a list of all Distribution Groups that cannot accept E-mail from external recipients.
The parameter that defines this option is – RequireSenderAuthenticationEnabled
In case that the value of the parameter – RequireSenderAuthenticationEnabled is – $True, the meaning is that the Distribution Group will accept E-mail from external (non-organization) recipients.
Display Distribution Groups that doesn’t accept E-mail from external recipients
PowerShell command example:
Get-DistributionGroup | Where {$_.RequireSenderAuthenticationEnabled -eq $False } | FT DisplayName,RequireSenderAuthenticationEnabled
Get information about Distribution Group membership of a specific user.
In this scenario, we want to get information about the Distribution Group,
which a specific user is a member of.
PowerShell command example:
$User = read-host “User Name"
$UserDName = (Get-Mailbox $User).name
"The User " + $User + " is a member of the following Distribution Groups:"
ForEach ($DistributionGroup in Get-Distributiongroup -resultsize unlimited)
{
if ((Get-Distributiongroupmember $DistributionGroup.identity | select -expand name) -contains $UserDName)
{$DistributionGroup.name}
}
Export information about Distribution Groups
The section which I describe “export information about Distribution Group” can be considered as a “derivative” of the section in which we use the PowerShell cmdlets “Get-DistributionGroup” or “Get-DistributionGroupMember” for fetching information about the Distribution Group infrastructure (get a list of existing Distribution Group, get information about group membership and so on).
Every information that we can display on the PowerShell console, can also be “exported” to various types of file formats.
File formats and export PowerShell cmdlets
PowerShell supports the option of exporting PowerShell command output to the following file formats: Text, CSV, HTML, and XML.
To be able to export the required information we need to tell PowerShell what is the “file format” that we want to use.
In addition to the PowerShell cmdlet’s that define the specific file format, PowerShell provides a dedicated “export command” for each of the file types and a unique parameter for a specific file type.
For example, when we wish to export PowerShell command output to a CSV (Comma Separated Value) file format, we can add additional parameters such as:
- –NoTypeInformation – this option prevents from PowerShell to add unnecessary information to the CSV file
- -Encoding UTF8 – in case that the objects such as users, or mailboxes include non-English characters, we can add this “format parameter” to enable PowerShell export information that includes non-English characters.
The “path” parameter
When we want to export information from a PowerShell command output to a file, we will need to provide PowerShell the exact path and the file name.
The PowerShell command that exports the data will use the “path information,” in creating a new file in the specified path.
It’s important to mention that when we provide a path, such as C:\Temp, the PowerShell command “except” that this path is already created. In other words, by default, the PowerShell command will not “create for us” a specific folder that was specified in the path.
Example of PowerShell syntax for exporting information to various file types
The following section, include a demonstration of exporting PowerShell command output to three types file formats.
To demonstrate the various file format, we use the PowerShell command-
Get-DistributionGroup that as the name implies, get information about existing Distribution Group and by default display the information on the PowerShell console.
In our scenario, we wish to change this default behavior and instead of displaying the information on the PowerShell console, we wish to “redirect” the PowerShell command output to a “File.”
Export information to Text FilePowerShell command example:
Get-DistributionGroup | Out-File c:\temp\DistributionGroups.TXT
Export information to CSV FilePowerShell command example:
Get-DistributionGroup | Export-CSV c:\temp\DistributionGroups.CSV –NoTypeInformation -Encoding UTF8
Export information to HTML File
PowerShell command example:
Get-DistributionGroup | ConvertTo-Html c:\temp\DistributionGroups.HTML
The task of exporting information about Distribution Group members to a file could look like a simple task, but this task required some “manipulation” of the PowerShell syntax that we use.
An example of PowerShell command syntax that we can use for exporting information about Distribution Group members to an SCV file could be:
Exporting information about Distribution Group members to a csv file
PowerShell command example:
$Groups = Get-DistributionGroup -ResultSize Unlimited
Foreach ($Group in $Groups)
{
$Members = Get-DistributionGroupMember -Identity $($Group.PrimarySmtpAddress)
Foreach ($Member in $Members)
{
Out-File -FilePath c:\temp\DistributionGroupMember.csv -InputObject "$($Group.DisplayName),$($Member.DisplayName),$($Member.PrimarySMTPAddress)" -Encoding UTF8 -append
}
}
The next article in the current article series
I’m pretty sure you have the logic on RequireSenderAuthenticationEnabled backwards.
Do you have anything on Removing users from distribution lists in BULK!