powershellexchange-serverexchange-server-2013exchange-management-shell

Clear mailboxes using Exchange Management Shell based on AD Logon Name (SAMAccountName?)


I have a CSV containing 150 - 200 AD logon names (SAMAccountNames?) and I need to loop through each user and clear their mailboxes.

I can do that using this command

​​Search-Mailbox -Identity "<MailboxOrMailUserIdParameter>" -DeleteContent -force

But the issue is that I need to be able to use the AD logon names, as I don't have the Identity?


Solution

  • If you are importing the SamAccountName and you used that to create each alias for your mailboxes, then I would do something like this:

    $names = get-content C:\path.of.csv
    
    foreach($name in $names){
         search-mailbox -identity $name -deletecontent -force
    }
    

    If your logon names do not match the alias of the mailbox, you need to figure out what attribute you used to populate that field. Then, you can use the same foreach loop to iterate through your AD objects to find the right attribute. Export that list and then try the search-mailbox using that.