locked
Deleting Stale Office 365 online Mailboxes with Powershell RRS feed

  • Question

  • We are using Office 365 online for our student email system.

    I want to remove any mailbox that hasn't been accessed in the last year.

    Thursday, November 20, 2014 11:04 PM

Answers

  • Once you determine that, you can remove the mailboxes a number of ways - depending on how your onsite AD interacts with Office 365.

    If you use DirSync, the users that have been deleted onsite should eventually be purged from Office 365, mailboxes included.

    *To be honest, I have always deleted users manually using the method below, so I cannot affirm, from experience, that this is the case, so someone may correct me.*

    You can manaully purge mailboxes as well (I had to do this a while ago) with PowerShell cmdlets.

    $cred = get-credential
    connect-msolservice -credential $cred
    Remove-MsolUser –UserPrincipalName user@contoso.com

    Yes, you have to install what is known as the Windows Azure Active Directory Module for PowerShell to use those commands (cmdlets).

    http://technet.microsoft.com/en-us/library/jj151815.aspx

    If that's not clear, please ask follow up questions.


    Please mark as helpful if you find my contribution useful or as an answer if it does answer your question. That will encourage me - and others - to take time out to help you.

    Friday, November 21, 2014 2:34 PM
  • More information here:

    http://technet.microsoft.com/en-us/library/dn186233(v=exchg.150).aspx

    So Remove-Mailbox is yet another option (if you are in a hybrid environment?).


    Please mark as helpful if you find my contribution useful or as an answer if it does answer your question. That will encourage me - and others - to take time out to help you.

    Friday, November 21, 2014 2:54 PM

All replies

  • Get-MailboxStatistics will tell you when a mailbox was last accessed.  You can run a script that gets all your mailboxes and feed that output to this command to find what you need.

    Get-Mailbox -ResultSize Unlimited | Sort Name | Get-MailboxStatistics | ? { $_.LastLogoffTime -lt (Get-Date).AddYears(-1) }

    That should return the information you need, assuming no one else has accessed these mailboxes in the meantime.

    Friday, November 21, 2014 1:53 PM
  • Once you determine that, you can remove the mailboxes a number of ways - depending on how your onsite AD interacts with Office 365.

    If you use DirSync, the users that have been deleted onsite should eventually be purged from Office 365, mailboxes included.

    *To be honest, I have always deleted users manually using the method below, so I cannot affirm, from experience, that this is the case, so someone may correct me.*

    You can manaully purge mailboxes as well (I had to do this a while ago) with PowerShell cmdlets.

    $cred = get-credential
    connect-msolservice -credential $cred
    Remove-MsolUser –UserPrincipalName user@contoso.com

    Yes, you have to install what is known as the Windows Azure Active Directory Module for PowerShell to use those commands (cmdlets).

    http://technet.microsoft.com/en-us/library/jj151815.aspx

    If that's not clear, please ask follow up questions.


    Please mark as helpful if you find my contribution useful or as an answer if it does answer your question. That will encourage me - and others - to take time out to help you.

    Friday, November 21, 2014 2:34 PM
  • More information here:

    http://technet.microsoft.com/en-us/library/dn186233(v=exchg.150).aspx

    So Remove-Mailbox is yet another option (if you are in a hybrid environment?).


    Please mark as helpful if you find my contribution useful or as an answer if it does answer your question. That will encourage me - and others - to take time out to help you.

    Friday, November 21, 2014 2:54 PM
  • Thanks for your answer.  I take it there isn't a way just to delete these non-used mailboxes.  I have 7000 mailboxes of which 6000 haven't ever been accessed.  If the mailbox is over a year old and hasn't been accessed in the last year I would like to delete them.
    Friday, November 21, 2014 3:35 PM
  • Use the PowerShell commands that David sent, and combine them with mine:

    $cred = get-credential
    connect-msolservice -credential $cred
    $UnUsedMailboxes = Get-Mailbox -ResultSize Unlimited |  ? { (Get-MailboxStatistics).LastLogoffTime -lt (Get-Date).AddYears(-1) }
    $UnUsedMailboxes | % {
        Remove-MsolUser –UserPrincipalName $_.PrimartSmtpAddress
    }

    That should be a good starting point ...


    Friday, November 21, 2014 4:20 PM