Replace existing distribution group ManagedBy using PowerShell and csv

Franco 0 Reputation points
2023-03-24T21:17:45.0266667+00:00

Trying to create PowerShell for Exchange Online ( running v3)

Need to add Owners (Managed By) to many DLs.

This would be a combination of multiple users and multiple distribution groups hence the need for CSV.

I have tried the following but it ends up replacing the last user on the list as the owner of the entire list of distribution groups :

$GroupList = Import-CSV C:\temp\ManagedBy.csv
ForEach ($group in $GroupList)
{
ForEach ($user in $GroupList)
{
}
Set-DistributionGroup -BypassSecurityGroupManagerCheck -Identity $group.GroupName -managedby $user.ManagedBy
}

I have looked all over but other "solutions" are offered but don't work for various reasons

Microsoft Exchange Online
Microsoft Exchange Online Management
Microsoft Exchange Online Management
Microsoft Exchange Online: A Microsoft email and calendaring hosted service.Management: The act or process of organizing, handling, directing or controlling something.
4,578 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,595 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Vasil Michev 108.1K Reputation points MVP
    2023-03-25T07:43:30.05+00:00

    ManagedBy is a multi-valued property, so you can use the hashtable notation to add/remove to the list, instead of overwriting it:

    Set-DistributionGroup -BypassSecurityGroupManagerCheck -Identity $group.GroupName -managedby @{add=$($user.ManagedBy)}

    For more details refer to the cmdlet help

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.