David
David

Reputation: 1

Removing Users from all AD Groups based on CSV File

Hi I am new to PowerShell, I have a CSV file of Active directory user names, I need to remove them from all their group memberships except the default "Domain Users"

I have this script but it doesn't work, and wondering if any one could advise me on what I am doing wrong.

Import-Module ActiveDirectory

$users = import-csv C:\temp\AD\groups_test.csv

Get-ADPrincipalGroupMembership $user| foreach {Remove-ADGroupMember $_ -Members $user -Identity 
Confirm:$false}

Upvotes: 0

Views: 3108

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174435

Assuming your CSV has a column SAMAccountName with the user's username, do this:

$users = Import-Csv C:\temp\AD\groups_test.csv

foreach($user in $users){
  Get-ADPrincipalGroupMembership $user.SAMAccountName |Remove-ADGroupMember -Members $user.SAMAccountName -Confirm:$false
}

PowerShell will automatically bind the ADGroup objects output by Get-ADPrincipalGroupMembership to Remove-ADGroupMember's -Identity parameter.

Upvotes: 1

Related Questions