Monday, August 6, 2012

Permanently Delete Office 365 MsolUser without waiting 30 Days

I have been working on a transition from Exchange 2010 to Office 365, doing a cutover conversion so that I can decommission my current Exchange Server.

The documentation, while there is a lot of it, is conflicting, misleading, and sometimes just wrong.

In any case, one of the most useful things I learned in this process was how to permanently delete MSOL users. Users that have been deleted and recreated with the same UserPrincipalName can cause all sorts of havoc.

Are you experiencing one of these problems:

  • DirSync reports:

Unable to restore user from a deleted state because of following error(s):
Value 'xx@yy.com' for property 'UserPrincipalName' conflicts with another object in the directory.
Value ‘xx@yy.com’ for property 'ProxyAddress' conflicts with another object in the directory.

  • Users can sign into https://portal.microsoftonline.com but not able to connect with a desktop client or mobile phone.
  • Multiple users show up in your Global Address List (GAL)
  • In Lync you see a the same user multiple times or you see deleted users

Well, all of these problems can be a result of deleting an account and recreating it with the same UserPrincipalName or ProxyAddress as the deleted version. Most documentation reports that the accounts will stick around for 30 days and there’s nothing you can do but wait or put a ticket into support. However, the latest version of MSOL PowerShell Tools has a fix—the latest is available from: http://onlinehelp.microsoft.com/Office365-enterprises/ff652560.aspx#BKMK_DownloadTheMOSIdentityFederationTool

The fix “Remove-MsolUser –RemoveFromRecycleBin –ObjectId ObjectId

If you don’t have the “-RemoveFromRecycleBin” parameter option, then you are not using the latest version of MSOL PowerShell Tools.

To see if this is affecting a particular user, do the following in MSOL PowerShell

Connect-MSOLService
Get-MsolUser –ReturnDeletedUsers –SearchString UserPrincipalName | select UserPrincipalName, ObjectId

The result will display all deleted mailbox containing the search string UserPrincipalName and their corresponding ObjectId’s.

To permanently delete one of the results, use the returned ObjectID in above statement in the Remove-MsolUser statement:

Remove-MsolUser -RemoveFromRecycleBin –ObjectId ObjectId

Note—this just deletes the already deleted mailbox from the recycling bin. It doesn’t do anything with the active (non-deleted) MsolUser if one happens to exist.

I ended up having a ton of deleted users from various failed attempts at migrating, and trying out SSO, so I wrote a simple PowerShell script to aid in the clearing out of deleted mailboxes. It should go without saying that if you do this, the deleted mailbox is no longer recoverable—it’s permanently deleted. For that reason, I have it show you the active user as a precaution so you can make sure the ObjectId that is about to be deleted permanently is not in fact the ObjectId of your currently active user. This script also prompts you twice to ensure you really want to permanently delete the MsolUser.

Param($searchString = $null)

function O365Logon
{
 #Must be ran from MSOL Shell
 Connect-MSOLService
}

function Main
{
 
 if ($searchString -eq $null) {
  $DeletedMailUsers = Get-MsolUser -ReturnDeletedUsers | select UserPrincipalName, ObjectId
 } else {
  $DeletedMailUsers = Get-MsolUser -SearchString $searchString -ReturnDeletedUsers | select UserPrincipalName, ObjectId
 }
 foreach($DeletedUser in $DeletedMailUsers) {
  Write-Host "The active account for" $DeletedUser.UserPrincipalName " is:"
  Get-MsolUser -UserPrincipalName $DeletedUser.UserPrincipalName | select ObjectId
  $a = Read-Host Delete $DeletedUser.UserPrincipalName $DeletedUser.ObjectId ('y/n')?
  if($a.ToLower() -eq 'y')
  {
   Remove-MsolUser -RemoveFromRecycleBin -ObjectId $DeletedUser.ObjectId
   Write-Host "Removed User " $DeletedUser.ObjectId
  }
 }

}

O365Logon
Main

To run this script, copy and save the script in notepad as PermanentlyDeleteADeletedAccount.ps1 and start an Administrative MSOL PowerShell session.

The command syntax is:

.\PermanentlyDeleteADeletedAccount.ps1
or
.\PermanentlyDeleteADeletedAccount.ps1 UserPrincipalName

12 comments:

  1. This is the exact issue we were experiencing. Good write up. Thanks

    ReplyDelete
  2. .Net Development platform includes of a development environment that enables .Net developers to easily and graphically build an program.

    .Net Development

    ReplyDelete
  3. Sweet! Thank you. Wasn't aware of the new -RemoveFromRecycleBin. Since we are in a POC phase of deployment, it would be handy to have -EmptyRecycleBin :)

    ReplyDelete
    Replies
    1. ...then again, there is a DIY "Empty Recycle Bin":
      Get-MsolUser -ReturnDeletedUsers | % {Remove-MsolUser -RemoveFromRecycleBin -objectid $_.ObjectID}

      Delete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Thank you very much. I had many bad accounts because of an improper sync. After deleting all the bad accounts, I was able to use Dir Sync with no problems.

    ReplyDelete
  6. Awesome post!

    Even the maroons at Microshaft didn't know this could be done. Glad to know I'm wasting my $$$ on a support contract.

    Worked like a charm, thanks again!

    ReplyDelete
  7. Hi
    we are using cutover to move mailbox from ex2010 to office 365
    the problem is:
    Only administrator mailbox moved
    External contacts and groups also moved
    Here is the problem:
    None of the mailboxes of other users move?
    Office 365 does not create any new users
    No deleted users in the recycle bin.
    The migration user have full access to the mailboxes.

    ReplyDelete
  8. The following does the same as your script but for all deleted users...

    Get-MsolUser -ReturnDeletedUsers | foreach { Remove-MsolUser -ObjectId $_.ObjectId -RemoveFromRecycleBin -Force }

    http://rebootrinserepeat.wordpress.com/2013/03/07/delete-all-deleted-users-in-office-365/

    Todd

    ReplyDelete
  9. Hey!
    Thank you very much for sharing this useful information. I was doing a project and for that I was looking for related information. Some of the points are very useful. Do share some more material if you have.

    Vachel
    .NET Development Chicago
    cmscentral.net

    ReplyDelete
  10. Get-MsolUser -ReturnDeletedUsers | Remove-Msoluser -RemoveFromRecycleBin -force

    That'll do it

    ReplyDelete
  11. Hey!
    What a commendable work you have done, with simplest of language. Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic.


    Vachel
    PHP Development Chicago
    .NET Development Chicago
    Chicago Development Team
    cmscentral.net

    ReplyDelete