问题
I have a CSV file with a list of user names, I need to delete all of these users from Active Directory using the Remove-ADObject command
. I am not very familiar with the syntax for this command - hoping you guys can help me here.
Import-Module activedirectory
$list = Import-CSV C:\Users\user\Desktop\deleteuserstest.csv
forEach ($item in $list) {
$samAccountName = $item.samAccountName
Remove-ADobject -Identity $samAccountName
}
回答1:
You have to use DN or GUID with Remove-ADObject. You can do something like this:
Import-Module ActiveDirectory
$list = Import-CSV C:\Users\user\Desktop\deleteuserstest.csv
forEach ($item in $list) {
$samAccountName = $item.samAccountName
#Get DistinguishedName from SamAccountName
$DN = Get-ADuser -Identity $Samaccountname -Properties DistinguishedName |
Select-Object -ExpandProperty DistinguishedName
#Remove object using DN
Remove-ADObject -Identity $DN
}
来源:https://stackoverflow.com/questions/44505430/i-need-to-delete-users-from-active-directory-using-a-imported-csv-file