none
How to bulk delete AD contacts from a csv list? RRS feed

  • Question

  • I'm having problems writing a script to delete all contacts listed in a csv file. I have tried few things and either get errors or nothing gets deleted.

    $ContactList= Import-Csv "c:\Users\test\desktop\Contacts.csv"
    foreach($C in $ContactList)
    {
    Get-ADObject -Filter "givenName -eq '$($C.Display)'"-SearchBase "OU=Contacts,DC=pi,DC=local" -Properties * | Select-Object -Property ObjectGUID | Remove-ADObject
    } 

    This doesn't give any errors and seems like it should work but nothing gets deleted. Other things I tried seem to find the users then prompt for an Identity.

    Any ideas? I'm new to PowerShell, created these contacts with a script I wrote and it was fairly simple but can't seem to delete them now.



    • Edited by clarky cat Sunday, August 9, 2015 7:00 PM
    Sunday, August 9, 2015 6:59 PM

Answers

  • Why are you fetching all properties? I would also add objectclass to the filter. You can try this:

    Import-Csv C:\Users\test\desktop\Contacts.csv | ForEach-Object {
        $Filter = "ObjectClass -eq 'Contact' -and GivenName -eq '$($_.Display)'"
        Get-ADObject -Filter $Filter -SearchBase "OU=Contacts,DC=pi,DC=local" | 
            Remove-ADObject -WhatIf
    }

    • Marked as answer by clarky cat Tuesday, August 11, 2015 8:37 PM
    Sunday, August 9, 2015 8:54 PM

All replies

  • Try it like this.

    "givenName" is not display name it is first name.  "givenName is NOT unique and will fail when more than one "john" or "susan" exist.

    you should use "Name"

    ContactList= Import-Csv "c:\Users\test\desktop\Contacts.csv"
    foreach($C in $ContactList){
        Get-ADObject -Filter "givenName -eq '$($C.Display)'"-SearchBase "OU=Contacts,DC=pi,DC=local" -Properties * | 
        Remove-ADObject -WhatIf
    } 



    \_(ツ)_/


    • Edited by jrv Sunday, August 9, 2015 9:11 PM
    • Proposed as answer by jrv Tuesday, August 11, 2015 8:43 PM
    Sunday, August 9, 2015 8:50 PM
  • Why are you fetching all properties? I would also add objectclass to the filter. You can try this:

    Import-Csv C:\Users\test\desktop\Contacts.csv | ForEach-Object {
        $Filter = "ObjectClass -eq 'Contact' -and GivenName -eq '$($_.Display)'"
        Get-ADObject -Filter $Filter -SearchBase "OU=Contacts,DC=pi,DC=local" | 
            Remove-ADObject -WhatIf
    }

    • Marked as answer by clarky cat Tuesday, August 11, 2015 8:37 PM
    Sunday, August 9, 2015 8:54 PM
  • Tis might be better:

    Import-Csv c:\Users\test\desktop\Contacts.csv |
    	ForEach-Object{
        	    if($contact=Get-ADObject -Filter "givenName -eq '$($_.Display)'"-SearchBase "OU=Contacts,DC=pi,DC=local"){
        		$contact|Remove-ADObject -WhatIf
        		Write-Host 'Removed Contact' -Fore green
        	    }else{
        		Write-Host "Contact not found $($_.Display)" -Fore red
        	    }
    	} 
    


    \_(ツ)_/


    • Edited by jrv Sunday, August 9, 2015 9:05 PM
    Sunday, August 9, 2015 9:05 PM
  • Try it like this.

    "givenName" is not display name it is first name.  "givenName is NOT unique and will fail when more than one "john" or "susan" exist.

    you should use "Name"

    ContactList= Import-Csv "c:\Users\test\desktop\Contacts.csv"
    foreach($C in $ContactList){
        Get-ADObject -Filter "givenName -eq '$($C.Display)'"-SearchBase "OU=Contacts,DC=pi,DC=local" -Properties * | 
        Remove-ADObject -WhatIf
    } 



    \_(ツ)_/


    Ah, I thought my script was potentially misleading. Actually the column on my csv title 'Display' was used to populate 2 values in the contact, the display name and the first name. I didn't use a surname so finding the givenName should get an exact match for the object.

    I did try Name at first as you suggested before posting but got errors,

    "Name -eq '$($C.Display)'"

    Would that be correct syntax? Either way in my test environment the object name, first name and display name are all the same.

    Tuesday, August 11, 2015 6:46 PM
  • Why are you fetching all properties? I would also add objectclass to the filter. You can try this:

    Import-Csv C:\Users\test\desktop\Contacts.csv | ForEach-Object {
        $Filter = "ObjectClass -eq 'Contact' -and GivenName -eq '$($_.Display)'"
        Get-ADObject -Filter $Filter -SearchBase "OU=Contacts,DC=pi,DC=local" | 
            Remove-ADObject -WhatIf
    }


    Probably a mixture of laziness and inexperience:) Will change that in future to just fetch the desired properties. Presume when running this in a live environment with 1000's of objects it will take a long time to process? In my lab it made no difference.
    Tuesday, August 11, 2015 7:00 PM
  • Why are you fetching all properties? I would also add objectclass to the filter. You can try this:

    Import-Csv C:\Users\test\desktop\Contacts.csv | ForEach-Object {
        $Filter = "ObjectClass -eq 'Contact' -and GivenName -eq '$($_.Display)'"
        Get-ADObject -Filter $Filter -SearchBase "OU=Contacts,DC=pi,DC=local" | 
            Remove-ADObject -WhatIf
    }


    Probably a mixture of laziness and inexperience:) Will change that in future to just fetch the desired properties. Presume when running this in a live environment with 1000's of objects it will take a long time to process? In my lab it made no difference.

    I see what you meant now, I was calling the all the properties and selecting GUID as when I read the help file it said how remove-adobject can only accept guid or DN as an input so I was thinking that I had to select either of those before piping to the cmdlet. Looks like that was the problem as it worked when that was removed and I added confrim:$false

    Thanks

    Tuesday, August 11, 2015 8:41 PM
  • Try it like this.

    "givenName" is not display name it is first name.  "givenName is NOT unique and will fail when more than one "john" or "susan" exist.

    you should use "Name"

    ContactList= Import-Csv "c:\Users\test\desktop\Contacts.csv"
    foreach($C in $ContactList){
        Get-ADObject -Filter "givenName -eq '$($C.Display)'"-SearchBase "OU=Contacts,DC=pi,DC=local" -Properties * | 
        Remove-ADObject -WhatIf
    } 



    \_(ツ)_/



    First answer posted...

    \_(ツ)_/

    Tuesday, August 11, 2015 8:43 PM