Set-DistributionGroup Script Help!

Hello, super humans!

I am trying to write a script that will remove Distribution Group Ownership for multiple users from multiple Distribution Groups, but I’m getting an error I don’t know how to resolve.

Here’s what I’m trying to do:

  1. There will be a list of users in File1. I want it to Select the DistinguishedName property, send that to a variable called “Owner”, then before it processes this command on the next user it will…
  2. Run another for each loop on File2, removing the current user (Step 1) from the ownership list of all groups in the file, then move on to the next user and run it all again.

The problem seems to be related to the type of information that is being stored in variable “Owner”. Below I will put my code, and then the error that I am getting. Please tell me what I’m doing wrong, and thanks in advance!

set-adserversettings -viewentireforest $True

$Groups = Get-Content "C:\Groups.txt"
$DN = Import-Csv "C:\DN.csv" 

ForEach($D in $DN)
{
    $D | Select -expandproperty DistinguishedName -OutVariable Owner
   
        ForEach ($Group in $Groups) 
        {
        Set-DistributionGroup $Group -Managedby((Get-DistributionGroup $Group).Managedby -= $Owner)
        }

}

The error I get is as follows:

Exception calling “op_Subtraction” with “2” argument(s): “Conversion from System.Collections.ArrayList to
Microsoft.Exchange.Data.Directory.ADObjectId has not been implemented.”
At C:\Script.ps1:14 char:49

  •     Set-DistributionGroup $Group -Managedby((Get-DistributionGroup $Group).M ...
    
  •                                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : NotSpecified: (:slight_smile: , MethodInvocationException
    • FullyQualifiedErrorId : DotNetMethodException

Use Set-DistributionGroup $Group -Managedby @{Remove=$Owner} syntax
https://technet.microsoft.com/en-us/library/bb124955(v=exchg.160).aspx

Hi, thanks for the reply! I tried running it with the above syntax, but now I get a different error. Here is what I see when running the command as follows:

set-adserversettings -viewentireforest $True

$Groups = Get-Content "C:\Groups.txt"
$DN = Import-Csv "C:\DN.csv" 

ForEach($D in $DN)
{
    $D | Select -expandproperty DistinguishedName -OutVariable Owner
   
        ForEach ($Group in $Groups) 
        {
        Set-DistributionGroup $Group -Managedby @{Remove=$Owner}
        
        
        }

}

The error I get is as follows:
All groups must have at least one owner who manages membership, message approval, and other settings for the group.
Make sure you assign an owner for this group.
+ CategoryInfo : NotSpecified: (:slight_smile: , RecipientTaskException
+ FullyQualifiedErrorId : A2C81D90
+ PSComputerName : etc-excas-01.corp.erac.com

There is only 1 user at a time in the $Owner variable, and the error would suggest it’s trying to remove all owners

the error say that you try to delete last group owner and leave the group without owners at all.
may be some of your groups have only one owner? Than you can’t just remove last owner, you mush assign to group new owner
something like

Set-DistributionGroup $Group -Managedby @{Remove=$Owner, Add=$newOwner}

Hi,

I dont know if you have received the solution but I would approach with the below steps:

  1. Export the list of distribution group alongwith the current owners :
    Get-DistributionGroup -resultsize unlimited | Select-object Name,@{label=”ManagedBy”;expression={[string]($.managedby | foreach {$.tostring().split(“/”)[-1]})}},Primarysmtpaddress | export-csv “c:managed by.csv”

This will ensure that you got a proper list of DL alongwith current owners.

  1. Create a CSV which will have two columns :
    DLName owner
    ABC Testuser1
    XYZ TestUser2
    Save the csv and then use the below command:
    Import-CSV “D:\DL.csv” | ForEach {Set-DistributionGroup -Identity $.DLName -Managedby $.Owner}

You can try step 2 for two DL’s which have multiple owners and if you think its working as expected, you can run for the rest.
Let me know if you got any queries.