none
Bulk delete mail contacts with specific email domain suffix

    Question

  • I need to delete thousands of contacts from Exchange Online with a specific domain suffix.  Does anyone know how I would do this using Powershell?  I can use  "Remove-MailContact -identity" to remove one address but can't figure out how to do a bulk action.

    Thanks

    Thursday, December 6, 2018 9:06 AM

Answers

  • Hi,

    Thanks for your question.

    1. Use pipeline to pipe the result.

    get-mailcontact -filter "emailaddresses -like '*mydomain.com'" | 
    Remove-MailContact -Confirm:$False

    Please follow Bofh's suggestion.

    Best Regards,

    Lee


    Just do it.

    • Marked as answer by TimIUK Friday, December 7, 2018 8:47 AM
    Friday, December 7, 2018 3:04 AM
    Moderator
  • Hi,

    Thanks for your question.

    1. Use pipeline to pipe the result.

    get-mailcontact -filter "emailaddresses -like '*mydomain.com'" | 
    Remove-MailContact -Confirm:$False  - Great

    Please follow Bofh's suggestion. - No time

    Best Regards,

    Lee


    Just do it.


    • Marked as answer by TimIUK Friday, December 7, 2018 8:47 AM
    • Unmarked as answer by TimIUK Friday, December 7, 2018 8:47 AM
    • Marked as answer by TimIUK Friday, December 7, 2018 8:47 AM
    Friday, December 7, 2018 8:47 AM

All replies

  • You can use Get-MailContact and pipe it to Remove-MailContact.

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Thursday, December 6, 2018 9:22 AM
  • You can use Get-MailContact and pipe it to Remove-MailContact.

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Thanks for your response.  This is what I've got so far.

    $DeleteContact = get-mailcontact -filter "emailaddresses -like '*mydomain.com'" 
    Remove-MailContact $DeleteContact -Confirm:$False

    But I get an error which says

    Cannot process argument transformation on parameter 'Identity'. Cannot convert the "System.Collections.ArrayList" value of type 
    "System.Collections.ArrayList" to type "Microsoft.Exchange.Configuration.Tasks.MailContactIdParameter".
        + CategoryInfo          : InvalidData: (:) [Remove-MailContact], ParameterBindin...mationException
        + FullyQualifiedErrorId : ParameterArgumentTransformationError,Remove-MailContact
        + PSComputerName        : outlook.office365.com

    Thursday, December 6, 2018 10:19 AM
  • I urgently recomend for you to start to learn the very basics of Powershell. You will avoid a lot of wasted time and frustration. You have to pipe the result of Get-MailContact to Remove-MailContact. This is the bottom line of Powershell scripting.

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Thursday, December 6, 2018 11:33 AM
  • I urgently recomend for you to start to learn the very basics of Powershell. You will avoid a lot of wasted time and frustration. You have to pipe the result of Get-MailContact to Remove-MailContact. This is the bottom line of Powershell scripting.

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    That's really helpful, thanks.

    Can anyone else help with my question?

    Thursday, December 6, 2018 11:36 AM
  • I urgently recomend for you to start to learn the very basics of Powershell. You will avoid a lot of wasted time and frustration. You have to pipe the result of Get-MailContact to Remove-MailContact. This is the bottom line of Powershell scripting.


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    That's really helpful, thanks.

    Can anyone else help with my question?

    ".. You have to pipe the result of Get-MailContact to Remove-MailContact."  Read it again, 3x .. or more
    Thursday, December 6, 2018 12:01 PM
  • Please take a look at the help topic about the pipeline I linked. Read at least the first 2 or 3 paragraphs and take a look at the examples. You will see it will help you to understand.

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Thursday, December 6, 2018 12:02 PM
  • Hi,

    Thanks for your question.

    1. Use pipeline to pipe the result.

    get-mailcontact -filter "emailaddresses -like '*mydomain.com'" | 
    Remove-MailContact -Confirm:$False

    Please follow Bofh's suggestion.

    Best Regards,

    Lee


    Just do it.

    • Marked as answer by TimIUK Friday, December 7, 2018 8:47 AM
    Friday, December 7, 2018 3:04 AM
    Moderator
  • Hi,

    Thanks for your question.

    1. Use pipeline to pipe the result.

    get-mailcontact -filter "emailaddresses -like '*mydomain.com'" | 
    Remove-MailContact -Confirm:$False  - Great

    Please follow Bofh's suggestion. - No time

    Best Regards,

    Lee


    Just do it.


    • Marked as answer by TimIUK Friday, December 7, 2018 8:47 AM
    • Unmarked as answer by TimIUK Friday, December 7, 2018 8:47 AM
    • Marked as answer by TimIUK Friday, December 7, 2018 8:47 AM
    Friday, December 7, 2018 8:47 AM
  • Please follow Bofh's suggestion. - No time

    That's why we love to help people like you!  ;-)

    You don't have time to spend 10 minutes to read something what probably helps you saving a lot of time in the future? Now it took even more time.  :-P

    Have a great day!


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Friday, December 7, 2018 9:10 AM