Welcome › Forums › General PowerShell Q&A › Remove a Distribution List Owner via powershell
- This topic has 8 replies, 4 voices, and was last updated 1 year ago by
Participant.
-
AuthorPosts
-
-
January 8, 2020 at 3:36 pm #197021
If I run the script to add a new admin to a DL, it works fine:
$dl = ‘[email protected]’
$Admin = ‘[email protected]’
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 -
January 8, 2020 at 3:47 pm #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
`-
This reply was modified 1 year ago by
Lars Panzerbjørn.
-
January 8, 2020 at 4:15 pm #197039
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
`
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.
-
This reply was modified 1 year ago by
-
January 8, 2020 at 4:55 pm #197051
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.-
This reply was modified 1 year ago by
Lars Panzerbjørn.
-
This reply was modified 1 year ago by
-
January 8, 2020 at 5:12 pm #197066
Thanks Lars.
Unfortunately it gives the same error on the line $List = $List – $New
Error: “does not contain a method named ‘op_Subtraction'” -
January 8, 2020 at 6:42 pm #197087
-
January 9, 2020 at 5:17 pm #197210
Thanks Olaf.
I finally got it to work.
$DL_Alias = ‘[email protected]’
$Admin_Alias = ‘[email protected]’
$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 $NewListAny 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.
-
January 9, 2020 at 8:37 pm #197267
Loop not required, just check if the object is in the array:
PowerShell1if ($list -notcontains (Get-RemoteMailbox $Admin_Alias).identity) {$NewList += $list} -
January 16, 2020 at 5:34 pm #198365
Loop not required, just check if the object is in the array
if ($list -notcontains (Get-RemoteMailbox $Admin_Alias).identity) {$NewList += $list}
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.
-
-
AuthorPosts
- The topic ‘Remove a Distribution List Owner via powershell’ is closed to new replies.