Microsoft 365 User Account Reporting Using Graph PowerShell

Managing and auditing user accounts in Microsoft 365 can be challenging, especially with a large number of users. Automating this process with Graph PowerShell can significantly streamline your administration tasks. Below is a detailed script to generate a comprehensive report of user accounts, including details such as Display Name, User Principal Name, Account Status, and Last Sign-In Date.


Prerequisites

  • Install the Microsoft Graph PowerShell module if not already installed by running Install-Module -Name Microsoft.Graph -Force -AllowClobber.
  • Authenticate with the required permissions using the following command:
    Connect-MgGraph -Scopes "User.Read.All" "Directory.Read.All" "AuditLog.Read.All"

PowerShell Script

# Install the Microsoft.Graph module if not already installed
Install-Module -Name Microsoft.Graph -Force -AllowClobber

# Connect to Microsoft 365
Connect-MgGraph -Scopes "User.Read.All" "Directory.Read.All" "AuditLog.Read.All"

# Define the output CSV file path
$outputFile = "C:\Reports\UserAccountReport.csv"

# Initialize an array to store user details
$userReport = @()

# Get all users
$users = Get-MgUser -All -Property DisplayName, UserPrincipalName, AccountEnabled, SignInActivity

# Process each user
foreach ($user in $users) {
    $userDetails = [PSCustomObject]@{
        DisplayName      = $user.DisplayName
        UserPrincipalName = $user.UserPrincipalName
        AccountEnabled   = $user.AccountEnabled
        LastSignInDate   = $user.SignInActivity.LastSignInDateTime
    }
    $userReport += $userDetails
}

# Export the report to a CSV file
$userReport | Export-Csv -Path $outputFile -NoTypeInformation

Write-Output "User account report generated: $outputFile"

# Disconnect from Microsoft 365
Disconnect-MgGraph

Script Explanation

Prerequisites:

  • Ensure the Microsoft.Graph module is installed and connected to your Microsoft 365 tenant.
  • The Connect-MgGraph command initiates a connection to Microsoft 365 with the required scopes for reading user and audit log information, including User.Read.All, Directory.Read.All, and AuditLog.Read.All.

Define Output File:

  • Specify the path for the output CSV file where the user account report will be saved.

Initialize Array:

  • Initialize an array to store user details.

Get All Users:

  • Retrieve all users with the properties DisplayName, UserPrincipalName, AccountEnabled, and SignInActivity.

Process Each User:

  • Loop through each user and create a custom object with the required details. Add each user's details to the array.

Export Report:

  • Export the array of user details to a CSV file at the specified path.

Disconnect:

  • The Disconnect-MgGraph command ends the session with Microsoft 365.

Enhancements

  • Add More User Details: Include additional user properties such as Job Title, Department, or License details in the report.
  • Filter Users: Add filters to generate reports for specific user groups, such as active users, disabled users, or users in a specific department.
  • Schedule the Script: Schedule the script to run at regular intervals using Windows Task Scheduler or Azure Automation to keep the report updated.
  • Email Report: Add functionality to email the report to administrators once it is generated.

Conclusion

Automating user account reporting in Microsoft 365 using Graph PowerShell simplifies administrative tasks, saving time and reducing the potential for human error. This script provides a straightforward way to generate comprehensive reports on user accounts, including key details like account status and last sign-in date. With further enhancements, this script can become an even more powerful tool in your administrative toolkit.

Embrace automation and streamline your Microsoft 365 management with the power of Graph PowerShell!


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