Remove a Distribution List Owner via powershell

If I run the script to add a new admin to a DL, it works fine:

$dl = ‘dl@abc.com’
$Admin = ‘admin@abc.com’
Set-DistributionGroup $dl -BypassSecurityGroupManagerCheck -ManagedBy((Get-DistributionGroup $dl).ManagedBy + $Admin)

But If I run this script to remove an admin it fails. I believe this worked in the past.
The error I get is “does not contain a method named ‘op_Subtraction’”

Set-DistributionGroup $dl -BypassSecurityGroupManagerCheck -ManagedBy((Get-DistributionGroup $dl).ManagedBy - $Admin)

How can I use powershell to remove an admin from a DL?
Thanks

This worked for me, but a long time ago, probably Exchange 2010 or so…

$Alias = ‘DL_ALL_In_Company’
$NewMGR = ‘Joe Smith’
$DG = Get-DistributionGroup $Alias
$List = $DG.ManagedBy
$New = Get-Mailbox $NewMGR
$List += $New
Set-DistributionGroup $Alias -ManagedBy $List

[quote quote=197027]This worked for me, but a long time ago, probably Exchange 2010 or so…

$Alias = ‘DL_ALL_In_Company’

$NewMGR = ‘Joe Smith’

$DG = Get-DistributionGroup $Alias

$List = $DG.ManagedBy

$New = Get-Mailbox $NewMGR

$List += $New

Set-DistributionGroup $Alias -ManagedBy $List

[/quote]
This script would add an owner. That part already works for me.

I need to be able to REMOVE an owner from a DL (not mailbox) via powershell.

 

Sorry, I misread. And if you do $List - $New?
IIRC, “ManagedBy” is a collection of objects (Mailboxes), and you are trying to remove a string.

Thanks Lars.

Unfortunately it gives the same error on the line $List = $List - $New
Error: “does not contain a method named ‘op_Subtraction’”

Remove a String from a Multi-Valued Attribute

Thanks Olaf.

I finally got it to work.

$DL_Alias = ‘dl@abc.com’
$Admin_Alias = ‘admin@abc.com’
$lists = (Get-DistributionGroup $DL_Alias).ManagedBy
$NewList = @()
foreach ($list in $lists) {if ($list -ne (Get-RemoteMailbox $Admin_Alias).identity){$NewList += $list}}
Set-DistributionGroup $DL_Alias -ManagedBy $NewList

Any way I could get this in a single line of code (other than the 2 valiables lines). I was hoping to pass variables via Excel.

If not that fine.

 

Loop not required, just check if the object is in the array:

if ($list -notcontains (Get-RemoteMailbox $Admin_Alias).identity) {$NewList += $list}

[quote quote=197267]Loop not required, just check if the object is in the array

if ($list -notcontains (Get-RemoteMailbox $Admin_Alias).identity) {$NewList += $list}

[/quote]

Thanks Rob. Unfortunately that does not remove the admin from the list. If the admin is in the list, all admins are stripped. Also I would still end up with 4 lines of code.