How to get the last sign in date of an Azure AD User in Powershell

Gabriel Gunnarsson,Azure ADGraph

In the Azure Portal you can see a users last sign in date on the overview page of a user. However, this property is not exposed by the Azure AD Powershell Module. (At least, I haven't found it.)

Fortunately this data can be retrieved using Microsoft Graph. (The Azure AD Powershell Module is also planned for deprecation.)

First off, make sure you have installed the Microsoft Graph Powershell SDK. (docs.microsoft.com: Install the Microsoft Graph PowerShell SDK)

Connect to Microsoft Graph

Connect-Graph -Scopes "User.Read.All"
Select-MgProfile "beta"

Retrieve last sign in date

$UPN = "Enter UPN here"
 
$user = Get-MgUser -Filter "userprincipalname eq '$UPN'" -Property SignInActivity
 
$user | Select DisplayName, UserPrincipalName, `
    @{Name='LastSignInDateTime'; Expression={$_.SignInActivity.LastSignInDateTime}}, `
    @{Name='LastNonInteractiveSignInDateTime'; Expression={$_.SignInActivity.LastNonInteractiveSignInDateTime}}
 

Retrieve all guests

The code snippet above will retrieve the value for a single user, but by changing the filter property you can, for example, retrieve the data of all guests.

$users = Get-MgUser -Filter "userType eq 'Guest'" -All -Property SignInActivity `
    | Select DisplayName, UserPrincipalName, `
    @{Name='LastSignInDateTime'; Expression={$_.SignInActivity.LastSignInDateTime}}, `
    @{Name='LastNonInteractiveSignInDateTime'; Expression={$_.SignInActivity.LastNonInteractiveSignInDateTime}}

Read more

Comments

© Gabriel Gunnarsson.RSS