Remove a Distribution List Owner via powershell

Welcome Forums General PowerShell Q&A Remove a Distribution List Owner via powershell

Viewing 7 reply threads
  • Author
    Posts
    • #197021
      Hil
      Participant
      Topics: 31
      Replies: 41
      Points: 215
      Rank: Participant

      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

    • #197027
      Participant
      Topics: 12
      Replies: 38
      Points: 261
      Helping Hand
      Rank: Contributor

      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

      • #197039
        Hil
        Participant
        Topics: 31
        Replies: 41
        Points: 215
        Rank: Participant

        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.

         

    • #197051
      Participant
      Topics: 12
      Replies: 38
      Points: 261
      Helping Hand
      Rank: Contributor

      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.

    • #197066
      Hil
      Participant
      Topics: 31
      Replies: 41
      Points: 215
      Rank: Participant

      Thanks Lars.

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

    • #197087
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP
    • #197210
      Hil
      Participant
      Topics: 31
      Replies: 41
      Points: 215
      Rank: Participant

      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 $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.

       

      • This reply was modified 8 months, 2 weeks ago by Hil.
      • This reply was modified 8 months, 2 weeks ago by Hil.
      • This reply was modified 8 months, 2 weeks ago by Hil.
      • This reply was modified 8 months, 2 weeks ago by Hil.
      • This reply was modified 8 months, 2 weeks ago by Hil.
    • #197267
      Participant
      Topics: 13
      Replies: 1753
      Points: 3,136
      Helping Hand
      Rank: Community Hero

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

    • #198365
      Hil
      Participant
      Topics: 31
      Replies: 41
      Points: 215
      Rank: Participant

      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.

Viewing 7 reply threads
  • The topic ‘Remove a Distribution List Owner via powershell’ is closed to new replies.