Tracking Daily Active Users in Microsoft 365 Using Graph PowerShell

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.


Prerequisites

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"

The Script

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"

Script Explanation

  1. Connect to Microsoft Graph: The script starts by connecting to Microsoft Graph with the required scopes (AuditLog.Read.All, Directory.Read.All, User.Read.All).
  2. Define Date Range: It sets the date range to the past 24 hours to track daily activity.
  3. Retrieve Users: The script fetches all users along with their SignInActivity property.
  4. Filter Active Users: It filters the users based on the SignInActivity property to find those who have signed in within the past day.
  5. Process Active Users: It creates a custom object for each active user and adds it to a list.
  6. Export to CSV: Finally, the script exports the list of active users to a CSV file named with the current date.

Note: Modify the date range to fit your tenant requirements.


Use Cases

  • User Engagement Analysis: Monitor which users are actively using Microsoft 365 apps to understand engagement levels.
  • License Utilization: Ensure that licensed applications are being effectively utilized by users.
  • Security Monitoring: Track user sign-ins to identify any unusual activity or potential security issues.

Possible Errors and Solutions

Empty CSV File

Cause: If the exported CSV file is empty, ensure the following:

  • The users have signed in to any Microsoft 365 apps within the past 24 hours.
  • The SignInActivity property is correctly populated for the users.
  • Necessary Microsoft 365 licenses are assigned to users for sign-in activity tracking.

Permission Issues

Cause: If you encounter permission errors while connecting to Microsoft Graph:

  • Ensure that the account used to run the script has the necessary permissions.
  • Use the correct scopes (AuditLog.Read.All, Directory.Read.All, User.Read.All) when connecting to Microsoft Graph.

Module Installation

Cause: If you face issues installing the Microsoft Graph PowerShell module:

  • Run PowerShell as an administrator.
  • Ensure you have an active internet connection to download the module.

Conclusion

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.


Related Articles:

Using Get-MgDirectoryRole in Graph PowerShell
Using Get-MgUserLicenseDetail in Graph PowerShell
Using Find-GraphMgCommand in Graph PowerShell
Connect to Microsoft 365 Using PowerShell
How to Create Bulk Users in Office 365 Using Graph PowerShell?
Create Microsoft 365 Group Using Microsoft Graph PowerShell
Block Microsoft 365 User Using Microsoft Graph PowerShell
Assign Microsoft 365 License Using Graph PowerShell
Microsoft 365 User Management Using Graph PowerShell
Checking Group Membership in Microsoft 365
Bulk Assign Microsoft 365 License
Find Inactive Users in Microsoft 365
Using Powershell Graph Search Query
Using Powershell Graph Filter Query
Using Where-Object In Graph PowerShell
Using Expand Property In Graph PowerShell
Using Select Object In Graph PowerShell
Using -Contains Operator In Graph PowerShell
Add User to Multiple Microsoft 365 Groups Using Graph PowerShell
Get Microsoft 365 User Location Using Graph PowerShell
Import Microsoft 365 Groups from CSV File Using Graph PowerShell
Microsoft 365 Group User Import Using Graph PowerShell

© m365corner.com. All Rights Reserved. Design by HTML Codex