Edit: Based on input from Scepticalist and TheStingPilot, I have cobbled together the following:
$users = import-csv usersfromgroups.csv
Foreach ($user in $users){
remove-adgroupmember -identity "$users.group" -members $users.username -WhatIf
}
This returns the following:
remove-adgroupmember : Cannot find an object with identity: ' .group' under: 'DC=domain,DC=com'.
At line:3 char:5
+ remove-adgroupmember -identity "$users.group" -members $users.use ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: ( .group:ADGroup) [Remove-ADGroupMember], ADIdentityNotFoundException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.RemoveADGroupMember
I'm not really sure how to proceed. Again, any help would be appreciated.
Original question:
I have a list of thirty or so users to be removed from a number of groups. Each user is to be removed from specific groups, and what groups they are to be removed from vary from user to user. I do not have a test environment. I have the list in CSV format, and it looks something like this:
username,group
User1,Group1
User1,Group2
User1,Group3
User2,Group4
User2,Group1
User3,Group2
User4,Group5
I've cobbled together the following, which doesn't look quite right to me:
$users = import-csv usersfromgroups.csv
$groups = import-csv usersfromgroups.csv
Foreach ($user in $users){
remove-adgroupmember -identity "$group" -members $user.username -Confirm:$true
}
Any help would be greatly appreciated.
Building on suggestions from the thread, I've got a script that works, assuming that your CSV contains the group in which the specified user is a member. If the user is a member of a group contained in the specified group, it doesn't work, but that is an issue for another day.
$users = import-csv usersfromgroups.csv
Foreach ($user in $users){
$group = Get-AdGroup $user.group
remove-adgroupmember -identity $group -members $users.username #-WhatIf
}