← script library

Automate Microsoft 365 User Report Generation with PowerShell

Microsoft 365 & Entra IDJanuary 9, 2025

In this post, we will look at a handy script that automates the generation of user reports in Microsoft 365. This script retrieves information about all users in your organization and exports it to a CSV file, making it easier for you to manage and analyze user data.

powershell
# Install the MSOnline module if its not already installed.
Install-Module -Name MSOnline -Force -AllowClobber
# Connect to Microsoft 365
$credential = Get-Credential
Connect-MsolService -Credential $credential

# Get all users and select relevant properties
$userList = Get-MsolUser | Select-Object DisplayName, UserPrincipalName, IsLicensed

# Export user details to a CSV file
$userList | Export-Csv -Path "C:\Users\YourUsername\Documents\M365UserReport.csv" -NoTypeInformation

# Disconnect from Microsoft 365
Disconnect-MsolService

Run it across your fleet

This script runs as-is on a single host. Paste it into ServerEngine to schedule it, run it on a whole server group in parallel, and keep the credentials out of the file — see the scripts documentation and the credential store.

Ready when you are.

Try ServerEngine free for 7 days.