I need to delete users from Active Directory using a imported csv file

末鹿安然 提交于 2019-12-08 07:32:41

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!