Manage M365 Users with PowerShell
Welcome to my website, where I share useful PowerShell scripts to optimize your workflow and productivity. 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.
### Step 1: Connect to Microsoft 365
The first step is to authenticate and establish a connection to Microsoft 365. Youll use the `Connect-MgGraph` cmdlet for this.
# Import necessary module Import-Module Microsoft.Graph # Connect to Microsoft 365 Connect-MgGraph -Scopes "User.Read.All"
### Step 2: Retrieve User Information
Once connected, use the `Get-MgUser` cmdlet to fetch user information. You can customize the selected properties according to your needs.
# Retrieve a list of all users $users = Get-MgUser -All | Select-Object DisplayName, UserPrincipalName, AccountEnabled # Output user list $users
### Step 3: Export User Data to CSV
Finally, you may want to export the user data to a CSV file for reporting or further processing.
# Export user data to a CSV file $users | Export-Csv -Path "M365Users.csv" -NoTypeInformation
By leveraging this script, you can efficiently manage your Microsoft 365 users. For a more advanced and reliable solution, consider using our software, ServerEngine, available at https://serverengine.co. Happy scripting!