Delete AD Group/Members

Ian Xue 37,711 Reputation points Microsoft Vendor
2020-07-16T03:17:28.263+00:00

Hi All

I have AD groups in csv file, i want to delete all the members of that groups. Will the below syntax help me.

Below is my csv file(there is space between Group and 01)

GroupName
Group 01
Group 02
Group 03

$groups=import-csv "C:\myadgroups.csv" 
foreach($group in $groups) 
{    
Get-ADGroupMember -Identity $group.GroupName | %{Remove-ADGroupMember -Identity $group.GroupName -Members $_.userprincipalname -Confirm:$false} 
} 

If i need to delete group members for a single AD group will the below syntax help me.

Get-ADGroupMember -Identity "Group 01" | %{Remove-ADGroupMember -Identity "Group 01" -Members $_.userprincipalname -Confirm:$false} 

If i need to delete AD groups will the below syntax help me

import-csv "C:\myadgroups.csv" | %{ Remove-ADGroup -Identity $_."GroupName" -Confirm:$false} 

https://social.technet.microsoft.com/Forums/windowsserver/en-US/4349528c-f27e-48fa-869e-50ac40c978fb/delete-ad-groupmembers?forum=winserverDS#7bb5c409-97cf-4caf-a1ae-3e2f9c1395de

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,561 questions
0 comments No comments
{count} votes

Accepted answer
  1. 2020-07-16T06:04:25.36+00:00

    Hi,

    Thanks for your question.

    Also, the parameter "-members" of the cmdlet "Remove-ADGroupmember" cmdlet can accept Distinguished Name, GUID, Sid or samaccoutnname. Not the userprincipalname you used.

    https://learn.microsoft.com/en-us/powershell/module/activedirectory/remove-adgroupmember?view=winserver2012-ps

    Best regards,

    Young Yang

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.