Tihomir Buncic
Tihomir Buncic

Reputation: 35

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?

Upvotes: 0

Views: 1754

Answers (1)

Pratik Jadhav
Pratik Jadhav

Reputation: 972

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

Upvotes: 0

Related Questions