简体   繁体   中英

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}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM