0
votes

I am trying to delete 100+ mailboxes from our cloud hosted Exchange server with PowerShell, but I keep getting this error:

The operation couldn't be performed because object '...' couldn't be found on '...'

The blanks are obviously an object from the array and our Exchange server. I am pulling the objects (I have tried both Email Addresses and Display Names) from a .csv file with the header 'mailbox'. I have used Disable-Mailbox and Remove-Mailbox, to no avail.

Here is my script:

Import-Csv "C:\temp\array.csv" | ForEach-Object {
    Remove-Mailbox -identity $_.mailbox -confirm:$false
}

I have been Googling all day and I can't seem to find the reason why these objects, that are in fact mailboxes in our Exchange environment, can't be found.

1
I would do a Get-Mailbox and pipe it into | Remove-Mailbox. Make sure to use -Whatif to make sure you are deleting what you intend to delete.jrider
Get-Mailbox will retrieve all mailboxes on our server though. How would I go about using an entire .csv list of email addresses as a condition that must be met in order to be deleted? It is surprising to me that an initial array of objects that I want deleted isn't enough.jdsr4c
As the docs explain: The Identity parameter identifies the mailbox that you want to remove. You can use any value that uniquely identifies the mailbox. For example: Name, Display name, Alias, Distinguished name (DN), Canonical DN, <domain name>\<account name>, Email address, GUID, LegacyExchangeDN, SamAccountName, User ID or user principal name (UPN). What identity value are you using in the CSV field mailbox?Theo

1 Answers

0
votes

What I am assuming is happening is that your CSV column of mailbox does not match the identity of the mailbox

You can use conditions to make sure that you are running Remove-mailbox on an actual mailbox:

#assuming that "array.csv" has a column named mailbox
Import-Csv "C:\temp\array.csv" | ForEach-Object {
    if((Get-Mailbox -Identity $_.Alias | Measure).Count -eq 1)
    {
        Write-Host "Removing "$_.mailbox -ForegroundColor Green
        Remove-Mailbox -Identity $_.mailbox -Confirm:$false #Here you should add -Whatif before running
    }
    else
    {
        Write-Host $_.mailbox " not found or not unique" -ForegroundColor Yellow
    }

}

Or, if you are confident that your query is correct, you can ignore the error by sticking with your original script and putting and -ErrorAction on Remove-Mailbox

Ex:

Import-Csv "C:\temp\array.csv" | ForEach-Object {
    Remove-Mailbox -identity $_.mailbox -confirm:$false -ErrorAction SilentlyContinue
}

To test if csv column mailbox matches the identity of the mailbox you can run a simple Get-Mailbox:

Import-Csv "C:\temp\array.csv" | ForEach-Object {
    Get-Mailbox -identity $_.mailbox 
}