← script library

Manage M365 Users with PowerShell

Microsoft 365 & Entra IDJanuary 9, 2025

Today, I present a script that helps you manage Microsoft 365 users efficiently. This script will allow you to retrieve a list of all users in your M365 environment, including their display names, email addresses, and user status.

powershell
# Import necessary module
Import-Module Microsoft.Graph
# Connect to Microsoft 365
Connect-MgGraph -Scopes "User.Read.All"

# Retrieve a list of all users
$users = Get-MgUser -All | Select-Object DisplayName, UserPrincipalName, AccountEnabled
# Output user list
$users

# Export user data to a CSV file
$users | Export-Csv -Path "M365Users.csv" -NoTypeInformation

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.