none
PS Scripting Error when trying to remove a user from multiple distribution lists RRS feed

  • Question

  • Good Evening,

    I'm having an issue when I try to delete 1 user from many distribution lists using power shell. I'm using the following command

    $DGs= Get-DistributionGroup | where { (Get-DistributionGroupMember $_ | foreach {$_.PrimarySmtpAddress}) -contains "user@domain.com"}
     
    foreach( $dg in $DGs){
    Remove-DistributionGroupMember $dg -Member user@domain.com
    }

    There is NO AD integration with my exchange online environment. 

    when I change the user@domain.com to the end user I receive the following error

     

    Cannot process argument transformation on parameter 'Identity'. Cannot convert 
    value "ACCESS Canadian Help Desk" to type 
    "Microsoft.Exchange.Configuration.Tasks.DistributionGroupMemberIdParameter". 
    Error: "Cannot convert hashtable to an object of the following type: 
    Microsoft.Exchange.Configuration.Tasks.DistributionGroupMemberIdParameter. 
    Hashtable-to-Object conversion is not supported in restricted language mode or 
    a Data section."
        + CategoryInfo          : InvalidData: (:) [Get-DistributionGroupMember],  
       ParameterBindin...mationException
        + FullyQualifiedErrorId : ParameterArgumentTransformationError,Get-Distrib 
       utionGroupMember
        + PSComputerName        : pod51048psh.outlook.com

    Can someone point me in the direction on where I'm going wrong.

    Willard Martin had proposed some answers but I couldn't replay back to his latest answer under dmarsh5. 

    Thanks for your assistance

    Wednesday, April 16, 2014 10:11 PM

Answers

  • If you just want to remove a single user from all distribution groups then this is the easiest way.

    Get-DistributionGroup -Filter * | Remove-DistributionGroupMember -WhatIf -Member userid  -Ea 0

    This will quietly remove the specified user from all distribution groups.


    ¯\_(ツ)_/¯

    Thursday, April 17, 2014 1:11 AM

All replies

  • Is this Office 365 you are trying to manage?


    ¯\_(ツ)_/¯

    Wednesday, April 16, 2014 10:35 PM
  • JRV,

    It is Office 365 that I am referring to. I already know what groups this former employee belongs to.

    I want to remove them from all their distribution lists using power shell. I know how to remove the user from individual distribution lists but I want to know how I can remove them from all distribution lists with one command.

    Darren

    Wednesday, April 16, 2014 11:18 PM
  • Hi,
    but I want to know how I can remove them from all distribution lists with one command.

    If you know the group names, you can just use a loop:

    Get-Content .\GroupNames.txt | ForEach {
    
        Remove-DistributionGroupMember -Identity $_ -Member user@domain.com
    
    }


    Don't retire TechNet! - (Don't give up yet - 12,830+ strong and growing)

    Wednesday, April 16, 2014 11:28 PM
  • If you just want to remove a single user from all distribution groups then this is the easiest way.

    Get-DistributionGroup -Filter * | Remove-DistributionGroupMember -WhatIf -Member userid  -Ea 0

    This will quietly remove the specified user from all distribution groups.


    ¯\_(ツ)_/¯

    Thursday, April 17, 2014 1:11 AM
  • Mike,

    This helped me out a lot.

    I'm trying to set script to not receive prompts so I tried adding the $ConfirmPreference but that did not work. I then tried Set-ExecutionPolicy but that also fails.

    I appreciate the assistance.

    Darren

    Thursday, April 17, 2014 1:38 PM
  • JRV,

    It looked like it was doing something but it never did remove the user from any of the distribution lists. I did not receive any errors though.

    Thanks

    Darren

    Thursday, April 17, 2014 1:39 PM
  • JRV,

    It looked like it was doing something but it never did remove the user from any of the distribution lists. I did not receive any errors though.

    Thanks

    Darren

    Remove the WhatIf from the command.  WhatIf prevents commands from actually doing anything for test purposes.


    ¯\_(ツ)_/¯


    • Edited by jrv Thursday, April 17, 2014 1:43 PM
    Thursday, April 17, 2014 1:43 PM
  • JRV,

    HUGE help!!

    Thanks so much for your time and patience.

    Darren

    Thursday, April 17, 2014 1:50 PM