powershellazure-active-directorymicrosoft-graph-apimicrosoft-graph-mail

Pulling "On-premises last sync date time" Azure user property using PowerShell


In Microsoft Entra admin center (https://aad.portal.azure.com/) we can pull the Azure AD user and could see "On-premises last sync date time" - time when the account was last time synced from OnPrem AD. I use MS Graph to get many properties but unable to check when the account was last time synced in hybrid environment. Is there a PowerShell command that would allow me to pull that property for a user account?


Solution

  • For Retrieving the "On-premises last sync date time" for a specific user:

    Connect-MgGraph -Scopes "User.Read.All"
    #Retrieve the "On-premises last sync date time" for a specific user:
    
    $user = Get-MgUser -UserId "<UserPrincipalName>" -Property "OnPremisesLastSyncDateTime"
    
    $user.OnPremisesLastSyncDateTime
    
    

    Output:

    enter image description here

    For Retrieving the “On-premises last sync date time” for a users who are on-prem synced:

    $users = Get-MgUser -All -Property "userPrincipalName,displayName,onPremisesLastSyncDateTime,onPremisesSyncEnabled"
    
    $syncedUserDetails = @()
    
    # Loop through each user and add details to the list if they are on-premises synced
    foreach ($user in $users) {
        if ($user.OnPremisesSyncEnabled -eq $true) {
            $syncedUserDetails += [PSCustomObject]@{
                UserPrincipalName = $user.UserPrincipalName
                DisplayName = $user.DisplayName
                OnPremisesLastSyncDateTime = $user.OnPremisesLastSyncDateTime
            }
        }
    }
    
    # Display the details of on-premises synced users
    $syncedUserDetails 
    
    

    Output:

    enter image description here