Search code examples
azurepowershellazure-powershellmicrosoft-entra-id

Get-AzureADUser with multiple pipes


Today I'm retrieving our Entra ID users using this code snippet:

Get-AzureADUser -All $true -Filter "UserType eq 'Member'"| Select-Object -Property $args[0] | Where-Object {($_.DirSyncEnabled -eq 'True')}

Later in my script I am using a code snippet I found on Google to retrieve license status for each user:

Get-AzureAdUser -ObjectId $Object.ObjectId | ForEach{ $licensed=$False;
 
For ($i=0; $i -le ($_.AssignedLicenses | Measure).Count ; $i++)
    { 
    If( [string]::IsNullOrEmpty( $_.AssignedLicenses[$i].SkuId ) -ne $True) { $licensed='Active' } 
    }; 
    If( $licensed -eq $false)
    { $licensed='Inactive'} 
}

But I would like to retrieve all data from Azure one time so I am trying to combine these two into one query.

If I just combine the pipe I doesn't retrieve any data. Just blank.

I was thinking of combining them both and instead of just putting the value into an variable add new psobject for each user. Like:

Get-AzureAdUser -ObjectId $Object.ObjectId | ForEach{ 

    $licensed=$False;
 
    For ($i=0; $i -le ($_.AssignedLicenses | Measure).Count ; $i++) { 
        If( [string]::IsNullOrEmpty( $_.AssignedLicenses[$i].SkuId ) -ne $True) { 
            $Object | Add-Member -MemberType NoteProperty -Name "LicenseStatus" -Value "Active" 
        } 
    }; 
    If( $licensed -eq $false) { 
        $Object | Add-Member -MemberType NoteProperty -Name "LicenseStatus" -Value "Inactive" 
    } 
}

Solution

  • To retrieve all Microsoft Entra ID users (synchronized from on-premises) along with their License status, make use of below PowerShell script:

    # Retrieve all Azure AD users who are members and synchronized from on-premises
    $users = Get-AzureADUser -All $true -Filter "UserType eq 'Member'" | Where-Object { $_.DirSyncEnabled -eq $true }
    
    $results = @()
    foreach ($user in $users) {
        $licensed = $false
       
        foreach ($license in $user.AssignedLicenses) {
            if (-not [string]::IsNullOrEmpty($license.SkuId)) {
                $licensed = 'Active'
                break
            }
        }
        
        if ($licensed -eq $false) {
            $licensed = 'Inactive'
        }
    
            $userObject = New-Object PSObject -Property @{
            DisplayName   = $user.DisplayName
            UserPrincipalName = $user.UserPrincipalName
            LicenseStatus = $licensed
        }
        
        $results += $userObject
    }
    
    $results
    
    

    enter image description here

    If you want to retrieve all Microsoft Entra ID users along with their License status, make use of below PowerShell script:

    # Retrieve all Azure AD users who are members 
    $users = Get-AzureADUser -All $true -Filter "UserType eq 'Member'" 
    
    $results = @()
    foreach ($user in $users) {
        $licensed = $false
       
        foreach ($license in $user.AssignedLicenses) {
            if (-not [string]::IsNullOrEmpty($license.SkuId)) {
                $licensed = 'Active'
                break
            }
        }
        
        if ($licensed -eq $false) {
            $licensed = 'Inactive'
        }
    
            $userObject = New-Object PSObject -Property @{
            DisplayName   = $user.DisplayName
            UserPrincipalName = $user.UserPrincipalName
            LicenseStatus = $licensed
        }
        
        $results += $userObject
    }
    
    $results
    

    enter image description here