Forum Discussion

mfranhind115's avatar
mfranhind115
Brass Contributor
Jul 12, 2022

powershell and Microsoft.Online: how to filter users with multiple conditions

Hi all,

I'm connected to my Tenant and several domains under it 

 

I would need to extract only user having

 

license O365_BUSINESS_ESSENTIALS or O365_BUSINESS_PREMIUM

 

and

 

not having ATP_ENTERPRISE

 

I started with a command like this:

 

Get-MsolUser | Where-Object {($_.licenses).AccountSkuId -match "O365_BUSINESS_PREMIUM"}

 

but I'm struggling to combine multiple conditions...

 

thanks for your help

  • mfranhind115 

     

    Here's an example using the Get-MsolUser commandlet you're using.

     

    Get-MsolUser is limited to client-side filtering making it less ideal for larger environments.

     

    I'd normally provide an example using server-side filtering, but this gets a bit more complicated in this instance so I'm going to skip it unless you're interested in this approach. Given all your other questions have remained aligned to the MSOnline module, this seems a safe assumption for now.

     

    Get-MsolUser |
        Where-Object {
            $Skus = $_.Licenses.AccountSku.SkuPartNumber;
            if ($Skus -notcontains "ATP_ENTERPRISE" -and ($Skus -contains "O365_BUSINESS_ESSENTIALS" -or $Skus -contains "O365_BUSINESS_PREMIUM")) { $true };
        }

     

    Cheers,

    Lain

  • LainRobertson's avatar
    LainRobertson
    Silver Contributor

    mfranhind115 

     

    Here's an example using the Get-MsolUser commandlet you're using.

     

    Get-MsolUser is limited to client-side filtering making it less ideal for larger environments.

     

    I'd normally provide an example using server-side filtering, but this gets a bit more complicated in this instance so I'm going to skip it unless you're interested in this approach. Given all your other questions have remained aligned to the MSOnline module, this seems a safe assumption for now.

     

    Get-MsolUser |
        Where-Object {
            $Skus = $_.Licenses.AccountSku.SkuPartNumber;
            if ($Skus -notcontains "ATP_ENTERPRISE" -and ($Skus -contains "O365_BUSINESS_ESSENTIALS" -or $Skus -contains "O365_BUSINESS_PREMIUM")) { $true };
        }

     

    Cheers,

    Lain

    • mfranhind115's avatar
      mfranhind115
      Brass Contributor

      EX-CEL-LENT Lain!!!!!!

      you won a couple of dinner my friend!!!!
      thanks a lot for your precious help

      I enjoied adding some info like this:

      Get-MsolUser | Select-Object UserPrincipalName, DisplayName, licenses, islicensed |
      Where-Object {
      $Skus = $_.Licenses.AccountSku.SkuPartNumber;
      if ($Skus -notcontains "ATP_ENTERPRISE" -and ($Skus -contains "O365_BUSINESS_ESSENTIALS" -or $Skus -contains "O365_BUSINESS_PREMIUM")) { $true };
      }

Resources