我需要使用导入的csv文件从Active Directory中删除用户

MMb*_*ill 1 powershell active-directory powershell-2.0 powershell-3.0

我有一个包含用户名列表的CSV文件,我需要使用以下命令从Active Directory中删除所有这些用户Remove-ADObject command.我对这个命令的语法不太熟悉 - 希望你们能在这里帮助我.

Import-Module activedirectory

$list = Import-CSV C:\Users\user\Desktop\deleteuserstest.csv

forEach ($item in $list) {
    $samAccountName = $item.samAccountName
    Remove-ADobject -Identity $samAccountName
}
Run Code Online (Sandbox Code Playgroud)

Nic*_*ick 5

您必须对Remove-ADObject使用DN或GUID.你可以这样做:

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
}
Run Code Online (Sandbox Code Playgroud)