Get list of users (not shared mailboxes) with or without license in Office365 using Powershell

5,126

If you supply account details on step 1, you just need to query AAD for a specific UPN you've just created with Get-MsolUser -UserPrincipalName user@domain which will return the object, along with licensing status.

Share:
5,126

Related videos on Youtube

Kjensen
Author by

Kjensen

Updated on September 18, 2022

Comments

  • Kjensen
    Kjensen over 1 year

    I need a method to get all user accounts in Office365 (synced from on-prem AD with AADSync), that are NOT shared mailboxes. I also need to know if they have a license, but I already know that bit.

    I need this to potentially assign them a license.

    The flow:

    1. A new AD account is created on premises
    2. It is synced with AADSync to AAD/Office365
    3. A process queries AAD for users and uses custom logic to determine, if a user should get a license assigned or not. The newly created account is returned and listed as having no license
    4. Process assigns license to new account, because a business rule says so

    How do I do step 3 here?

    I currently use something like this:

    Get-MsolUser -MaxResults Unlimited | Select-Object UserPrincipalName,IsLicensed,UsageLocation,Licenses
    

    ...But this also returns shared mailboxes, which I do not want.

    • Admin
      Admin over 7 years
      I'm not entirely sure I understand what you are trying to do, the powershell command you have listed returns all users, so I'm guessing you have some more code. Try this to see if it gets you what you want. PS C:\> Get-MsolUser | Select-Object UserPrincipalName,IsLicensed,UsageLocation,Licenses | foreach {$_.UserPrincipalName} {Get-Mailbox -ErrorAction SilentlyContinue -identity $_.UserPrincipalName -Filter {(-not(RecipientTypeDetailsValue -eq 'SharedMailbox')) -and (-not(RecipientTypeDetailsValue -eq 'RoomMailbox')) -and (-not(RecipientTypeDetailsValue -eq 'EquipmentMailbox'))}}