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.
Install-Module -Name Microsoft.Graph -Force -AllowClobber
.Connect-MgGraph -Scopes "User.Read.All" "Directory.Read.All" "AuditLog.Read.All"
# 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
Prerequisites:
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:
Initialize Array:
Get All Users:
Process Each User:
Export Report:
Disconnect:
Disconnect-MgGraph
command ends the session with Microsoft 365.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!
© m365corner.com. All Rights Reserved. Design by HTML Codex