MGGraph is a must to have in the toolbox when working with Azure AD. This can for example be used to see activity of guest accounts.

Install and connect MGGraph

First you need to install the PowerShell commands needed to run commands. The prerequisites are better described by Microsoft here .

Start PowerShell and run:

Install-Module Microsoft.Graph -Scope CurrentUser
Import-Module Microsoft.Graph

Export LastSignInDateTime to a CSV file

Now we will run a simple script to export user information that includes lastsignindatetime for all accounts. Remember to accept the consents when the sign-in prompt shows. You can skip the checkbox to accept for organization. Make sure you have a C:\temp folder or change the export value in the script.

Connect-MgGraph -Scopes AuditLog.Read.All, Organization.Read.All,
Select-MgProfile beta

$users = get-mguser

$signininfo = @()


#Loop for all users
Foreach ($u in $users) {
    $signininfo += Get-MgUser -UserId $u.Id -Property displayName,userPrincipalName,usertype,accountenabled,signInActivity,createdDateTime,department,title | select displayName,userPrincipalName,usertype,accountenabled,createdDateTime,department,title,@{Name = 'LastSignInDateTime'; Expression = {$_.SignInActivity.LastSignInDateTime}}

$signininfo |Export-Csv -Path C:\Temp\Signininfo.csv -NoTypeInformation -Encoding UTF8


Thanks for reading this blog.