Hi

Hoping someone can help with this script I have developed to remove all the members from a group without deleting the group. I think I have some kind of data conversion issue. Not an expert on PS so not sure what I should be doing now.

Thanks in advance for your help.

#Get members of group
$objDGMembers = Get-DistributionGroupMember -Identity “Group Name”

#For each memberin gorup
Foreach ($objMember in $objDGMembers)

{
Remove-DistributionGroupMember -Identity “Group Name” -Confirm:$False -Member $objMember
}

Getting this error

Cannot process argument transformation on parameter ‘Member’. Cannot convert value “joe.bloggs” to type

“Microsoft.Exchange.Configuration.Tasks.GeneralRecipientIdParameter”. Error: “Cannot convert hashtable to an object of the following type:
Microsoft.Exchange.Configuration.Tasks.GeneralRecipientIdParameter. Hashtable-to-Object conversion is not supported in restricted language mode or a Data section.”

  • CategoryInfo : InvalidData: (:slight_smile: [Remove-DistributionGroupMember], ParameterBindin…mationException
  • FullyQualifiedErrorId : ParameterArgumentTransformationError,Remove-DistributionGroupMember
  • PSComputerName : pod51036psh.outlook.com
4 Spice ups
Get-DistributionGroupMember -Identity 'GroupName' | foreach {Remove-DistributionGroupMember -Identity 'GroupName' -Member $_.distinguishedname -whatif }

Remove the -Whatif parameter after you’ve run this once and made sure it grabs the users you intended.

2 Spice ups

Have you checked to see if the output from your Get statement is the output you need for the Remove-DGMember? From the error it looks like it is just pulling the username, and not the full email address.

Try to use $objMember.distinguishedname although I don’t see anything wrong with your syntax.

Edit: too slow.

1 Spice up

The problem with the original syntax is that Get-DistributionGroupMember returns a deserialized object which is what you are passing to Remove-DistributionGroupMember’s -Identity parameter. If you look at the help for this cmdlet it will show you what properties you can pass to it. Distinguishedname is one, but you can’t pass the entire object to it, just a string.

2 Spice ups

Matt

Thanks, this worked for me

Get-DistributionGroupMember -Identity ‘GroupName’ | foreach {Remove-DistributionGroupMember -Identity ‘GroupName’ -Member $_.distinguishedname -Confirm:$False }

1 Spice up