Office 365 remove Mail users (external users) not active for a year

Welcome Forums General PowerShell Q&A Office 365 remove Mail users (external users) not active for a year

Viewing 0 reply threads
  • Author
    Posts
    • #206208
      Participant
      Topics: 4
      Replies: 0
      Points: 49
      Rank: Member

      Maybe this can help someone. I found out that get-MailUser gives the external users in Office 365 and that the WhenChanged property changes when they login. This is not the case for the normal mailbox user. Then you need to get mail statistics to get the last login time.

      I have made a small command that will calculate the date for a year ago and remove the Mail-user that has not been active in a year with no confirmation. I have this scheduled to run every month.

      $DateYearAgo = (Get-Date).AddDays(-365)
      Get-MailUser -ResultSize Unlimited -Filter "WhenChanged -lt '$DateYearAgo'" | select Identity | Remove-MailUser -Confirm:$false

      I get an error when i run this but the user is removed when i check:

      An Azure Active Directory call was made to keep object in sync between Azure Active Directory and Exchange Online. However, it failed. Detailed error message: Resource 'XXXXX-053c-49a9-a7cc-XXXXXXXXXXXX' does not exist or one of its queried reference-property objects are not present.

      If you have tips for me to make this better then i appreciate this.

       

       

Viewing 0 reply threads
  • The topic ‘Office 365 remove Mail users (external users) not active for a year’ is closed to new replies.