How to get users Last sign-in via powershell

JordanT 10 Reputation points
2023-09-04T11:20:51.96+00:00

Hello,

I am trying to load the users Last sign-in date/times as these are displayed in Azure AD, for example:

User's image

And trying to get this with microsofr.graph Get-MgUser.

I am loading the SignInActivity.LastSignInDateTime but the value returned is not matching the date that I see for this user on Azure AD as the attached image. Is there another attribute that I should check instead?

I am rather new with powershell so I would appreciate any ideas.

Get-MgUser -All
$Properties = @(
    'Mail', 'AccountEnabled', 'SignInActivity'  
)
 
#Get All users along with the properties
$AllUsers = Get-MgUser -All -Property $Properties
 Write-Output $AllUsers

# Perform Search
$AllUsers | ForEach-Object {
        $LastLoginDate = $AllUsers.SignInActivity.LastSignInDateTime         
        $_ |add-Member -MemberType Noteproperty -Name LastLoginDate -Value $LastLoginDate
    }

#From $users displays only alias and LastLogonTime properties
 $AllUsers | ft DisplayName,LastLoginDate
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,470 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,013 questions
{count} votes

2 answers

Sort by: Most helpful
  1. JordanT 10 Reputation points
    2023-09-04T12:15:44.94+00:00

    Solved, had a typo in the loop that returned the same date

    instead of

    $AllUsers.SignInActivity.LastSignInDateTime   
    

    I should have used $_.SignInActivity.LastSignInDateTime this works and returns the correct date

    1 person found this answer helpful.
    0 comments No comments

  2. JordanT 10 Reputation points
    2023-09-04T12:16:25.3+00:00

    Solved, had a typo in the loop that returned the same date

    instead of

    SQL

    $AllUsers.SignInActivity.LastSignInDateTime   
    

    I should have used $_.SignInActivity.LastSignInDateTime this works and returns the correct da

    1 person found this answer helpful.