Manage Distribution Groups by using PowerShell ~ o365info.com Manage Distribution Groups by using PowerShell
Sunday, August 26, 2012

The Exchange online ECP (Exchange Control Panel) enables us to execute management task relating to Distribution Groups in easy and convent way. But dissipate that fact, we can use PowerShell advantage for tasks that cannot be done by using the ECP interface such as: creating and managing Dynamic distribution group, a task such as: export information about group membership or create Bulk operations that influence more than one Distribution Group
In this article we review some of the basic management tasks related to Distribution Groups management.


Information and help related to PowerShell


1

PowerShell Naming Conventions &  general information
If you want to get more information about the Naming Conventions that we use in this article and, get some general tips about how to work with the PowerShell described in the article, read the article:  Help and additional information - o365info.com PowerShell articles

2

PowerShell command and Script languish in more details
If you are new in the PowerShell world, you can read more information about PowerShell in an office environment in the article: The Power of PowerShell

3

Create remote PowerShell session
Before we can use the required PowerShell commands, we need to download and install the office 365 cmdlets + create remote PowerShell session to office 365 or Exchange online. If you need more information about how to create a remote PowerShell session read the following articles: Part 2: Connect to Office 365 by using Remote PowerShell and Part 3: Connect to Exchange online by using Remote PowerShell

4

How to use a PowerShell script
Most of the PowerShell articles include a PowerShell script that simplify the use of the PowerShell commands. If you want to get more information about: How to use a PowerShell script, read the article: Connect to office 365 and Exchange online using a script


PowerShell commands table

1 - Create and delete Distribution Groups

Create a Distribution Group

PowerShell command syntax:
New-DistributionGroup -Name "<Distribution Group Name>"
Example:
New-DistributionGroup -Name DL-USA

Create a Distribution Group + Details

PowerShell command syntax:
New-DistributionGroup -Name "<Distribution Group Name>" -DisplayName "<Distribution Group Display name>"
-Alias "<Distribution Group Alias>" -PrimarySmtpAddress "<Distribution Group Email address>"
Example:
New-DistributionGroup -Name DL-USA -DisplayName DL-USA -Alias DL-USA -PrimarySmtpAddress DL-USA@o365info.com

Delete (Remove) a Distribution Group

PowerShell command syntax:
Remove-DistributionGroup "<Distribution Group Name>"
Example:
Remove-DistributionGroup DL-USA

2 - Manage Distribution Groups General Settings

Add user to a Distribution Group

PowerShell command syntax:
Add-DistributionGroupMember
"<Distribution Group Name>" -Member <User Name>
-BypassSecurityGroupManagerCheck
Example:
Add-DistributionGroupMember -Identity DL-USA -Member John
-BypassSecurityGroupManagerCheck

Setting Distribution Groups to accept Senders outside of my organization

PowerShell command syntax:
Set-DistributionGroup "<Distribution Group Name>"
-RequireSenderAuthenticationEnabled $False
Example:
Set-DistributionGroup -Identity DL-USA
-RequireSenderAuthenticationEnabled $False

Adding Distribution Group owner

PowerShell command syntax:
Set-DistributionGroup -Identity "<Distribution Group Name>" –ManagedBy <User Name>
-BypassSecurityGroupManagerCheck
Example:
Set-DistributionGroup -Identity DL-USA -ManagedBy John
-BypassSecurityGroupManagerCheck

Set a specific user as owner of all office 365 (BULK Mode)

PowerShell command syntax:
Get-DistributionGroup |Set-DistributionGroup -ManagedBy <User Name> –BypassSecurityGroupManagerCheck
Example:
Get-DistributionGroup |Set-DistributionGroup -ManagedBy “John
–BypassSecurityGroupManagerCheck

Add an Email Alias to Distribution group

PowerShell command syntax:
Set-DistributionGroup "<Distribution Group Name>"
-EmailAddresses SMTP:<Primary Email>,<Alias Email>
Example:
Set-DistributionGroup "DL-USA" –EmailAddresses
SMTP:DL-USA@o365info.com,dev@o365info.com

Hide Distribution group from GAL

PowerShell command syntax:
Set-DistributionGroup "<Distribution Group Name>"
-HiddenFromAddressListsEnabled $True
Example:
Set-DistributionGroup "dl-01" -HiddenFromAddressListsEnabled $True

Create new Security Distribution Group

PowerShell command syntax:
New-DistributionGroup -Name <Distribution Group Name>
-Type "Security"
Example:
New-DistributionGroup -Name "All Users" -Type "Security"
Adjustments & Customizations
Set Email Address for the Distribution Group:
New-DistributionGroup -Name "All Users"  -Type "Security"
-PrimarySmtpAddress AllUsers@o365info.com

Import Distribution Group members from a CSV File

CSV Sample

Import Distribution Group members from a CSV File

PowerShell command syntax:

Import-Csv <Path>| foreach {Add-DistributionGroupMember -Identity <Distribution Group Name> -Member $_.identity}
Example:
Import-Csv C:\Temp\DL-Users.csv | foreach {Add-DistributionGroupMember -Identity DL-USA -Member $_.identity}

Create bulk Distribution Groups from a CSV File

CSV Sample

Create bulk Distribution Groups from a CSV File

PowerShell command syntax:

Import-CSV <Path>| foreach {New-DistributionGroup -Name $_.name -Type $_.Type}
Example:
Import-CSV C:\Temp\DL-Group.csv | foreach {New-DistributionGroup -Name $_.name -Type $_.Type}

Add recipient to multiple distribution groups

PowerShell command syntax:

$arr = "<DL Name>","<DL Name>","<DL Name>"
foreach ($item in $arr)
{
Add-DistributionGroupMember -Identity $item –Member <Alias> –BypassSecurityGroupManagerCheck
}
Example:
$arr = "DL-01","DL-02","DL-03"
Foreach ($item in $arr)
{
Add-DistributionGroupMember -Identity $item -Member John
–BypassSecurityGroupManagerCheck
}

3 - Assign Permissions to Distribution Groups

Assign “Send As” Permissions to Distribution Group

PowerShell command syntax:
Add-RecipientPermission "<Distribution Group Name>"
-Trustee <User Name> -AccessRights SendAs -Confirm:$False
Example:
Add-RecipientPermission DL-USA -Trustee John -AccessRights SendAs -Confirm:$False

Assign “Full Access” permissions to Distribution Group + Automapping

PowerShell command syntax:
$DL = Get-distributiongroupmember <Distribution Group>| Select-Object -ExpandProperty Name
Foreach ($Member in $DL )
{Add-MailboxPermission -Identity <User>  -User $Member
-AccessRights ‘FullAccess’ -InheritanceType all}
Example:
$DL = Get-distributiongroupmember Assistants-group | Select-Object -ExpandProperty Name
foreach ($Member in $DL )
{Add-MailboxPermission -Identity FL1-Room1 -User $Member
-AccessRights ‘FullAccess’ -InheritanceType all}

Additional reading :
Auto-mapping shared mailboxes in Exchange 2010 SP1 with Outlook 2010 and Outlook 2007

4 - Manage Dynamic Distribution Groups

Create Dynamic Distribution Group for all Office 365 users

PowerShell command syntax:
New-DynamicDistributionGroup
-Name "<Distribution Group Name>" –RecipientFilter
{ (RecipientType -eq 'UserMailbox') }
Example:
New-DynamicDistributionGroup -Name DL-USA –RecipientFilter
{ (RecipientType -eq 'UserMailbox') }

Create Dynamic Distribution Group for user from specific office

PowerShell command syntax:
New-DynamicDistributionGroup
-Name "<Distribution Group Name>" -RecipientFilter {(RecipientType -eq 'UserMailbox') -and (Department
–like <'Department Name'>)}
Example:
New-DynamicDistributionGroup -Name "DEV" -RecipientFilter {(RecipientType -eq 'UserMailbox') -and (Department -like 'NY')}

Create Dynamic Distribution Group for all managers

PowerShell command syntax:
New-DynamicDistributionGroup
-Name "<Distribution Group Name>" -RecipientFilter {(RecipientType -eq 'UserMailbox') -and (Title –like <'Title1*'> -or Title -like <'Title2*'>)}
Example:
New-DynamicDistributionGroup -Name Org Managers -RecipientFilter {(RecipientType -eq 'UserMailbox') -and (Title -like 'Director*' -or Title -like 'Manager*' )}
Additional Reading

5 - Display Information about Distribution Groups

Display Distribution Group Members

PowerShell command syntax:
Get-DistributionGroupMember "<Distribution Group Name>"
Example:
Get-DistributionGroupMember DL-USA

Display members of Dynamic Distribution Group

PowerShell command syntax:
$DDG = Get-DynamicDistributionGroup "<Dynamic Group Name>" Get-Recipient -RecipientPreviewFilter $DDG.RecipientFilter |ft alias
Example:
$DDG = Get-DynamicDistributionGroup DL-USA Get-Recipient
-RecipientPreviewFilter $DDG.RecipientFilter |ft alias

Display list of Distribution Groups

PowerShell command syntax:
Get-DistributionGroup

Display list of Distribution Groups with specific Email Domain name suffix

PowerShell command syntax:
get-DistributionGroup | where {$_.emailaddresses –like <"*Domain Name*">} | ft -Property Name, alias ,EmailAddresses -autosize
Example:
Get-DistributionGroup | where {$_.emailaddresses -like "*o365info.com*" } | ft -Property Name, alias ,EmailAddresses
-autosize

Display information about Distribution Group that was updated before a specific date

PowerShell command syntax:
Get-DistributionGroup | Where{$_.WhenChanged -gt ((Get-Date).AddMonths(-<Number of Month>))}
Example:
Get-DistributionGroup | Where{$_.WhenChanged -gt ((Get-Date).AddMonths(-1))}
Additional Reading
Now it’s Your Turn!
We really like to know what is your opinion on the Article

{ 10 comments... read them below or Comment }

  1. I have a distribution group that I created but did not set it as a security group. Now I need to set it as a security group. What is the powershell command to change this distribution group and add the option of being a security group?
    I have not found the line item needed to add this option.
    Set-DistributionGroup -Name "HDGroup"
    But this is as far as I have gotton and not sure what the syntax is for changing the distribution group to include the security switch which is only available when you create the distribution list in the gui.

    ReplyDelete
  2. As far as I know, it is not possible to change a distributiongroup from one type to another. You may need to redo the process. At least in the GUI it has to be done that way.

    ReplyDelete
  3. This script works for Office 365 and removes the single user from all Distribution Groups. It does go through all groups and fails on the ones where the user is not a member, but it's quick and dirty.

    $email= read-host -prompt "Email Address"
    $DGs= Get-DistributionGroup

    foreach( $dg in $DGs){
    Remove-DistributionGroupMember $dg.name -Member $email -confirm:$false

    }

    ReplyDelete
  4. get-distributiongrupmember not recognised

    ReplyDelete
  5. Looking for something that can list distrobution groups a user is a member of...

    Kinda like this:
    $User = read-host -Prompt "Email Address"

    $user_dn = (Get-MsolUser -userprincipalname $user)

    "User " + $User + " is a member of the following groups:"

    foreach ($group in Get-Msolgroup -MaxResults 10000){ if ((Get-MsolGroupMember $group.identity | select -expand distinguishedname) -contains $user_dn){$group.name} }

    but cannot make it work

    ReplyDelete
  6. Hello,

    Need some ideas to automate adding or removing members in outlook with Shell scripting. Can i get the script please.

    Thanks in advance.

    ReplyDelete
  7. Hello,

    Need some ideas to automate adding or removing members in distribution group in outlook. Help me with the script.

    Thanks

    ReplyDelete
  8. Bill

    Very Good, thanks, do you know how to use the -and statement.

    EG:
    get-distributiongroup |where {$_.OrganizationalUnit -like "*namehere*" -and $_.OrganizationalUnit -like "*othernamehere*"}

    as it does not work. I need to identify groups from certain org unit names

    ReplyDelete
  9. Is there a power shell command that will hide or deny the sending to all Distribution Groups from 1 Distribution group?
    In other words, I have 1 Distribution group, that I do not want to be able to send to any other Distribution groups, nor do I want them to see Distribution Groups within the GAL.

    Thanks

    ReplyDelete

About

Recent posts

Popular Post

- Copyright © o365info.com -Metrominimalist- Powered by Blogger - Designed by Johanes Djogan -