Are you looking to keep track of your Office 365 users’ sign-in activity? Exporting user activity reports is a fantastic way to monitor their engagement or ensure compliance. In this guide, we’ll walk you through the steps to export the last sign-in date for all Office 365 users using PowerShell with the Microsoft Graph PowerShell SDK. This method is recommended for its modern approach and efficiency.
Step 1: Connect to Microsoft Graph PowerShell
To get started, you’ll first need to install the Microsoft Graph PowerShell SDK if you haven’t already:
Install-Module Microsoft.Graph -Force
If you already have it installed, make sure it’s up to date by running:
Update-Module Microsoft.Graph
Step 2: Connect with Appropriate Permissions
Next, you must connect to Microsoft Graph with the necessary permissions. Use the following command:
Connect-MgGraph -Scopes "AuditLog.Read.All", "User.Read.All"
You’ll be prompted to enter your Office 365 Tenant credentials and grant access.
Step 3: Export the Last Sign-In Activity Report
Now it’s time for the actual exporting. Below are the commands you’ll need to run to gather the last sign-in information per user:
$Users = Get-MgUser -All -Property Id, UserPrincipalName, DisplayName, UserType, SignInActivity, Mail, AccountEnabled |
Select-Object Id, UserPrincipalName, DisplayName, UserType, Mail, AccountEnabled,
@{n = "LastSuccessfulSignInDateTime"; e = { $_.SignInActivity.LastSuccessfulSignInDateTime } },
@{n = 'LastNonInteractiveSignInDateTime'; e = { $_.SignInActivity.LastNonInteractiveSignInDateTime } },
@{n = 'LastSignInDateTime'; e = { $_.SignInActivity.LastSignInDateTime } },
@{n = 'DaysSinceLastSuccessfulSignIn'; e = { (New-TimeSpan -Start $_.SignInActivity.LastSuccessfulSignInDateTime -End (Get-Date)).Days } }
$Users | Out-GridView -Title "Last sign-in activity report"
$Users | Export-Csv -Path "C:\temp\LastSignInActivity.csv" -NoTypeInformation -Encoding utf8
This script will create a report containing valuable information, such as:
- Id
- UserPrincipalName
- DisplayName
- UserType
- AccountEnabled
- LastSuccessfulSignInDate
- LastNonInteractiveSignInDateTime
- LastSignInDateTime
- DaysSinceLastSuccessfulSignIn
Step 4: Viewing the Report
After exporting, an Out-GridView will pop up, allowing you to search and filter through the properties easily. To view the exported data, simply open the CSV file with an application like Microsoft Excel. It provides a neatly organized view of the sign-in activity report.
Conclusion
Tracking Office 365 user activity can be invaluable for maintaining security and compliance. By following the steps above, you can efficiently export user sign-in information. If you need assistance with Office 365 management or making the most out of your cloud services, request a quote from System Support. Our managed IT services can help ensure your business is running smoothly!