
You can use below code to remove an AD group from the list of users specified in upnlist.txt file. Create a text file and add users UPN Names in it. You can modify the script to use user’s samAccount as well if you have users SAM Account Names instead of UPN Names.
Example:
upnlist.text file
user1@technethub.com
user2@technethub.com
user3@technethub.com
Update the <GroupName> to the AD Group which you want to remove from the users AD Group membership.
Best Practice: Test the script on one or two test users before running it all the users in your upnlist text file.
Download below script from Microsoft TechNet Gallery
1 2 3 4 5 6 7 8 9 10 11 |
<#RemoveUserfromGroup.ps1 Written by Jatin Makhija October 1,2019 upnlist.txt = Contains list of user's UPN e.g. xyz@technethub.com xyz1@technethub.com <GroupName> = This group will be removed from the user's mentioned in the upnlist.txt file #> $upnlist = Get-Content 'c:\temp\upnlist.txt' Foreach ($upn in $upnlist) { $var = Get-ADUser -Filter "UserPrincipalName -eq '$upn'" Remove-ADgroupmember -identity '<GroupName>' -members $var -Confim:$false } |