MMbill
MMbill

Reputation: 67

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

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
}

Upvotes: 1

Views: 8586

Answers (1)

Nick
Nick

Reputation: 1208

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
}

Upvotes: 6

Related Questions