Find the last logon time with Microsoft Graph

chusse45 1 Reputation point
2022-08-08T13:05:30.407+00:00

I have a CsV file with about 1000 users, I want to find their Azure AD last logon time. I've tried multiple commands with PowerShell but that was unsuccessful. I figure that MSGraph can be more useful. How do I import the csV that has the UPN and then get those UPN last logon with MSGraph?

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,255 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Vicky Kumar (Mindtree Consulting PVT LTD) 1,166 Reputation points Microsoft Employee
    2022-08-09T07:17:47.417+00:00

    Hi ,
    as you said you tried with multiples command and its failed, this might be due to a known a issue (if you were getting error : The term 'Get-AzureADAuditSignInLogs' is not recognized as the name of a cmdlet go with step 1 first ) ,make sure to unload the AzureAD module, then load the AzureADPreview one , then go with step 2, sample command .

    1st step

    Remove-module azuread
    Install-Module AzureADPreview -AllowClobber -Force

    similar problem - https://learn.microsoft.com/en-us/answers/questions/772918/index.html

    Step 2

    Import-Module AzureADPreview
    $Cred = Get-Credential
    Connect-MsolService -Credential $Cred
    Connect-AzureAD -Credential $Cred

    $Users = Get-MsolUser -all
    $Headers = "DisplayNametUserPrincipalNametLicensetLastLogon" >>C:\Mylist.csv ForEach ($User in $Users) { $UPN = $User.UserPrincipalName $LoginTime = Get-AzureAdAuditSigninLogs -top 100 -filter "userprincipalname eq '$UPN'" | select CreatedDateTime $NewLine = $User.DisplayName + "t" + $User.UserPrincipalName + "`t" + $LoginTime.CreatedDateTime
    $NewLine >>'C:\Mylist.csv'
    }

    You can change the name based on your CSV file and header title . if you got any error ,kindly share the screenshot of the error .

    Hope this will work, please let me know if you have any question .

    Thanks