Monitoring the daily active users in your Microsoft 365 tenant is crucial for understanding user engagement and ensuring the effective utilization of licensed applications. This article provides a comprehensive PowerShell script to track the daily active users based on their sign-in activity.
Before running the script, ensure you have the following prerequisites:
Install the Microsoft Graph PowerShell module:
Install-Module Microsoft.Graph -Scope CurrentUser
Connect to Microsoft Graph:
Connect-MgGraph -Scopes "AuditLog.Read.All", "Directory.Read.All", "User.Read.All"
Below is the PowerShell script to track the list of daily active tenant users:
# Connect to Microsoft Graph with required scopes
Connect-MgGraph -Scopes "AuditLog.Read.All", "Directory.Read.All", "User.Read.All"
# Define the date range for daily activity
$endDate = Get-Date
$startDate = $endDate.AddDays(-1)
# Get all users with their sign-in activity
$users = Get-MgUser -All -Property Id, DisplayName, UserPrincipalName, SignInActivity
# Debug output: total users retrieved
Write-Output "Total users retrieved: $($users.Count)"
# Filter users with sign-in activity within the last day
$activeUsers = $users | Where-Object {
$_.SignInActivity -ne $null -and ($_.SignInActivity.LastSignInDateTime -gt $startDate)
}
# Debug output: total active users filtered
Write-Output "Total active users in the last day: $($activeUsers.Count)"
# Create a list to hold the results
$activeUserList = @()
# Process each active user
foreach ($user in $activeUsers) {
$userSignIn = [pscustomobject]@{
Id = $user.Id
DisplayName = $user.DisplayName
UserPrincipalName = $user.UserPrincipalName
LastSignInDateTime= $user.SignInActivity.LastSignInDateTime
}
$activeUserList += $userSignIn
}
# Export the active users to a CSV file
$csvPath = "C:\ActiveTenantUsers_$($endDate.ToString('yyyyMMdd')).csv"
$activeUserList | Export-Csv -Path $csvPath -NoTypeInformation
Write-Output "The list of active users has been exported to $csvPath"
AuditLog.Read.All
, Directory.Read.All
, User.Read.All
).SignInActivity
property.SignInActivity
property to find those who have signed in within the past day.Note: Modify the date range to fit your tenant requirements.
Cause: If the exported CSV file is empty, ensure the following:
SignInActivity
property is correctly populated for the users.Cause: If you encounter permission errors while connecting to Microsoft Graph:
AuditLog.Read.All
, Directory.Read.All
, User.Read.All
) when connecting to Microsoft Graph.Cause: If you face issues installing the Microsoft Graph PowerShell module:
Tracking daily active users in your Microsoft 365 tenant is essential for understanding user engagement and optimizing license utilization. The provided PowerShell script leverages Microsoft Graph to retrieve sign-in activity data and export a list of active users. By following the steps outlined in this article, you can effectively monitor and analyze user activity in your organization.
© m365corner.com. All Rights Reserved. Design by HTML Codex