none
Need to delete multiple contacts in AD RRS feed

  • Question

  • I need to delete multiple contacts in AD. I found the script below, but when running and using "display"as the column title in the csv file. 

    when testing with a contact names"ron", the script seems to target anything with "ron" in it. Also, it also doesn't seem to be deleting the contacts.  Any thoughts on a better workign script? I'm not much of a scripter though.

    thanks

    What if: Performing operation "Remove" on Target "CN=ron

    Import-Csv E:\scripts\delete_multiple_contacts\Contacts.csv |
    ForEach-Object{
           if($contact=Get-ADObject -Filter "givenName -eq '$($_.Display)'"-SearchBase "OU=mail,DC=global,DC=<domain>,DC=com"){
        $contact|Remove-ADObject -WhatIf
        Write-Host 'Removed Contact' -Fore green
           }else{
        Write-Host "Contact not found $($_.Display)" -Fore red
           }

    • Moved by Bill_Stewart Monday, September 11, 2017 3:39 PM Abandoned
    Wednesday, July 12, 2017 3:23 PM

All replies

  • The script only returns contacts with given name as "ron".  There may be many contacts with the first name "ron"

    The way to check is:

    Get-AdObject -Filter "givenName -eq 'ron'" | select name, givenname, surname

    See how many are returned.

    The problem here is not PowerShell but is a lack of knowledge about AD.


    \_(ツ)_/

    Wednesday, July 12, 2017 4:14 PM
  • Your command returned all names with "ron" in it.

    Would you know how to configure this script so that I can delete all contacts in the csv file, and just those names only. not variations of the names?

    thanks!

    Wednesday, July 12, 2017 5:08 PM
  • First names can be duplicated many times in AD>  You need to use the CN (Name) attribute and the OU or the full distinguished name.to retrieve the correct account.

    Get-AdUser -Filter "Name -eq 'ron'" -SearchBase 'OU=mail,DC=global,DC=<domain>,DC=com' | select name, givenname, surname

    The CN must be unique within a container.  The GivenName does not have to be unique.


    \_(ツ)_/

    Wednesday, July 12, 2017 5:15 PM
  • The CN=0ron, givenname=ron. I think CN would be best. I'm also targeting a specific OU for the contacts.

    When running the command below i get no output. I tried it against Ron, 0ron and rdefino_contact. I did set my domain name when running it.

    Get-AdUser -Filter "Name -eq 'ron'" -SearchBase 'OU=mail,DC=global,DC=<domain>,DC=com' | select name, givenname, surname

    Wednesday, July 12, 2017 5:42 PM
  • CN and Name are the same thing.  Use either so this would be:

    CN=0ron, givenname=ron

    Get-AdUser -Filter "Name -eq '0ron'" -SearchBase 'OU=mail,DC=global,DC=<domain>,DC=com' | select name, givenname, surname


    \_(ツ)_/

    Wednesday, July 12, 2017 5:45 PM
  • Am I missing something? :)  I get no output when running this?
    Wednesday, July 12, 2017 5:57 PM