How to add/remove allow sender of distribution group with powershell?

Copper Contributor

Hello,

Please help me about how to add/remove allow sender of distribution group using Powershell?

I did find a few answers like the one below:

 

set the allow list users addresses:

Set-DistributionGroup "group name" –AcceptMessagesOnlyFrom email address removed for privacy reasons,email address removed for privacy reasons


add the specific users to allow list:

Set-DistributionGroup "group name" –AcceptMessagesOnlyFrom @{add="email address removed for privacy reasons","email address removed for privacy reasons"}


remove the specific users to allow list:

Set-DistributionGroup "group name" –AcceptMessagesOnlyFrom @{remove="email address removed for privacy reasons","email address removed for privacy reasons"}


use the following command to check the allow list users:

Get-distributiongroup -identity "group name" | fl acceptmessagesonlyfrom


Reminder: we need to type "" in the command in powershell. 

 

 

However, when I run the command, I get this error:

 

PS C:\WINDOWS\system32> Set-DistributionGroup "All Security Insurance" AcceptMessagesOnlyFrom @{add="email address removed for privacy reasons"}
Set-DistributionGroup : A positional parameter cannot be found that accepts argument 'AcceptMessagesOnlyFrom'.
At line:1 char:1
+ Set-DistributionGroup "All Security Insurance" AcceptMessagesOnlyFrom @{a ...
+ CategoryInfo : InvalidArgument: (:) [Set-DistributionGroup], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Set-DistributionGroup

 

 

I did connect to the exchange using:  Connect-ExchangeOnline

(Import-Module ExchangeOnlineManagement)
Btw, I use the standard Office 365 portal - it is not on-premises Office.

I would be grateful for your help, please.

Thank you

Sebastian

5 Replies

@sebgal to add a user as a sender to a DL , try the below 

 

# Connect to Exchange Online
$UserCredential = Get-Credential
Connect-ExchangeOnline -UserPrincipalName $UserCredential.UserName

# Replace "DistributionGroupName" with the actual name of your distribution group
$GroupName = "DistributionGroupName"
$NewSender = "email address removed for privacy reasons"

# Add the new sender to the distribution group
Add-DistributionGroupMember -Identity $GroupName -Member $NewSender

# Disconnect from Exchange Online
Disconnect-ExchangeOnline -Confirm:$false

 

 

 

To remove as sender from DL, try the below 

 

# Connect to Exchange Online
$UserCredential = Get-Credential
Connect-ExchangeOnline -UserPrincipalName $UserCredential.UserName

# Replace "DistributionGroupName" with the actual name of your distribution group
$GroupName = "DistributionGroupName"
$SenderToRemove = "email address removed for privacy reasons"

# Remove the sender from the distribution group
Remove-DistributionGroupMember -Identity $GroupName -Member $SenderToRemove -Confirm:$false

# Disconnect from Exchange Online
Disconnect-ExchangeOnline -Confirm:$false

@eliekarkafyMany thanks for getting back to me.

Your script functions excellently if I intend to add or remove a user to or from the Distribution List.

I've given it a go, and it's certainly proven to be very helpful - saved for future reference. :)

However, it's of particular importance for me to add a user (who is already a member of this Distribution List) to the group of senders permitted to email this distribution group email. Kindly refer to the screenshot below. I attempted to achieve this through the Office 365 Exchange portal, but I encountered an error: "Argument passed in is not serializable. Parameter name: value."

That's the reason I'm attempting to use PowerShell to accomplish this task instead.

 

 

sebgal_0-1692034406840.png

 

@sebgal you can do that using powershell 

 

$UserCredential = Get-Credential
Connect-ExchangeOnline -UserPrincipalName <YourAdminEmail> -ShowProgress $true -UserCredential $UserCredential

Add-DistributionGroupMember -Identity "DistributionListName" -Member "email address removed for privacy reasons", "email address removed for privacy reasons"

Disconnect-ExchangeOnline -Confirm:$false

Thank you, however when I run this script, I get this error:

cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
Credential
Connect-ExchangeOnline : A parameter cannot be found that matches parameter name 'UserCredential'.
At C:\Users\sebga\Documents\3.ps1:2 char:89
+ ... email address removed for privacy reasons -ShowProgress $true -UserCredential $UserCr ...
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Connect-ExchangeOnline], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Connect-ExchangeOnline

Add-DistributionGroupMember : The term 'Add-DistributionGroupMember' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the
path is correct and try again.
At C:\Users\sebga\Documents\3.ps1:3 char:1
+ Add-DistributionGroupMember -Identity "All Movo Insurance" -Member "l ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Add-DistributionGroupMember:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
This is the script I used:

$UserCredential = Get-Credential
Connect-ExchangeOnline -UserPrincipalName email address removed for privacy reasons -ShowProgress $true -UserCredential $UserCredential
Add-DistributionGroupMember -Identity "All Movo Insurance" -Member "email address removed for privacy reasons"
Disconnect-ExchangeOnline -Confirm:$false