Get the Last Login Date and Time of a SharePoint Online User using PowerShell
Requirement: Find the last login date of a user in SharePoint Online.
How to Get the Last Login Timestamp of a User in SharePoint Online?
Do you need to know when someone last logged into your company’s SharePoint Online site? It can be helpful for troubleshooting issues, security purposes, or just keeping track of who is using the site. There are a few ways to find this information, depending on what you need. This article will show you how to view the login history for SharePoint Online users. Even better, we’ll show you how to get the last login timestamp of a user with PowerShell!
In my case, To track a user’s activity, we had to find out the last logon. SharePoint doesn’t store the last sign-in timestamps anywhere. So, here are the available options:
Option 1: Find the SharePoint Online User’s Last Sign-in Time from Audit Logs
Let’s find out when the user last logged in to identify inactive users in SharePoint Online with the help of audit logs.
- Login to Microsoft 365 compliance center at https://compliance.microsoft.com
- Click on “Audit” from the left navigation >> Set the date, user, and Activity as “User Logged in” and click on the “Search” button.
- This will get you all the Sign-in activities of the selected user.
Option 2: Get the Last Login Date from Azure AD Console
The option #2 and option #3 methods discussed here give the login timestamps of an Office 365 user in general – and not specific to SharePoint Online.
- Login to Azure AD console at https://aad.portal.azure.com
- Click on the “Users” from left navigation >> Search and find the target user >> Click on the user name to open user profile page.
- You can see the last sign-in date as highlighted. On clicking the “User Sign-ins” link, you can find all sign-in events of the particular user.
You can also use the Microsoft 365 Admin center to get the last sign-in date and time stamps of any user. Please note, this will be the last sign-in time stamp to any of the Office 365 apps, but not limited to SharePoint Online alone.
Option 3: Using PowerShell to Get User’s Last Login Date
Use this PowerShell script to get a specific user’s last login date and time. This works for External users as well.
Import-Module AzureADPreview #Connect to Azure AD Connect-AzureAD -Credential (Get-Credential) #Get the Last Login Date and Time Get-AzureADAuditSignInLogs -Filter "userPrincipalName eq 'email@example.com'" -Top 1
This cmdlet retrieves the login information about a user. To export Sign in audit logs of all users in the environment to a CSV file, use the following:
Get-AzureADAuditSignInLogs -All $true | Export-CSV "C:\Temp\AzureAD-AuditSignInLogs.CSV" -NoTypeInformation
The Get-AzureADAuditSignInLogs cmdlet comes with AzureADPreview module. So, make sure you have installed it before running this script.
In conclusion, it is possible to retrieve the last login date for a user in SharePoint Online using the methods explained above. This can be useful for a variety of purposes, such as tracking user activity, identifying inactive users, or determining the effectiveness of training programs.