Ultimate Guide for Using Get-MgUser Cmdlet

Managing users in Microsoft 365 is an essential task for administrators. Whether it’s assigning licenses, updating user details, or troubleshooting, user management forms the backbone of any organization’s productivity ecosystem.

While the Microsoft 365 Admin Center provides a graphical interface for managing users, relying solely on it for repetitive or bulk tasks can be tedious. This is where Microsoft Graph PowerShell comes in, offering a robust, scriptable way to automate and streamline user management tasks.

In this article, we’ll focus on the Get-MgUser cmdlet, a powerful command that allows admins to retrieve and filter user information in Microsoft 365.

What Are Microsoft 365 Users?

Microsoft 365 users are accounts that allow individuals to access services like Exchange Online, SharePoint, Teams, and OneDrive. These users can be employees, contractors, or even service accounts used by applications.

Administrators typically manage these accounts using the Microsoft 365 Admin Center, where they can:

  • Create or delete users.
  • Reset passwords.
  • Assign licenses.
  • Manage group memberships.
While the Admin Center is user-friendly, it’s not always efficient for bulk operations or advanced filtering. This is where Get-MgUser becomes invaluable.

Why Use Get-MgUser?

The Get-MgUser cmdlet is part of the Microsoft Graph PowerShell module. It allows administrators to:

  • Automate repetitive user management tasks.
  • Fetch detailed user data programmatically.
  • Filter users based on specific attributes like licenses or statuses.
By integrating Get-MgUser into scripts, admins can save time, reduce manual effort, and minimize errors.

Setting Up Microsoft Graph PowerShell

Before using the Get-MgUser cmdlet, you need to set up the Graph PowerShell module.

1. Install the Module

Run this command in PowerShell to install the module:

Install-Module Microsoft.Graph -Scope CurrentUser

2. Connect to Microsoft Graph

Once installed, connect to your Microsoft 365 tenant:

Connect-MgGraph -Scopes "User.Read.All"

3. Disconnect After Use

When finished, disconnect to secure your session:

Disconnect-MgGraph  

Exploring the Get-MgUser Cmdlet

The Get-MgUser cmdlet retrieves user data from Microsoft 365. Whether you need information about all users, specific users, or filtered subsets, this cmdlet offers flexibility and precision.

Cmdlet Syntax

Get-MgUser [-UserId <String>] [-Filter <String>] [All]

Let’s break down its usage with practical examples.

Practical Examples of Get-MgUser

  1. Retrieve All Users
  2. To fetch all users in your Microsoft 365 tenant, use:

    Get-MgUser -All  

    This command retrieves a complete list of users, including their display names, email addresses, and UPNs.

  3. Fetch a Single User by User ID
  4. To get detailed information about a specific user by their unique ID:

    Get-MgUser -UserId "<UserId>"   

    This command retrieves a complete list of users, including their display names, email addresses, and UPNs.

  5. Fetch a Single User by UPN
  6. Instead of a user ID, you can fetch user details using the user principal name (email):

    Get-MgUser -UserId "john.doe@contoso.com"   

    This is especially helpful when you don’t have the user ID handy.

  7. Filter Users Based on Specific Criteria
  8. To retrieve all enabled users, use the -Filter parameter:

    Get-MgUser -Filter "accountEnabled eq true" -All  

    This command fetches users with active accounts. You can modify the filter for different attributes, like department or job title.

  9. Nesting Get-MgUser with Other Cmdlets
  10. You can combine Get-MgUser with other cmdlets to fetch personal user info like UserPrinicpalName, DisplayName etc. For example, retrieving group owners and fetching their personal info:

    Note: By default, Get-MgGroupOwner fetches only the User IDs.
    # Retrieve the detailed information of group owners  
    $owners = Get-MgGroupOwner -GroupId 'd420874b-edf7-4648-b0a0-07c254c376aa'  
    foreach ($owner in $owners) {  
        Get-MgUser -UserId $owner.Id | Select-Object DisplayName, Mail, UserPrincipalName  
    }
                                                
    This script first retrieves the owners of a specific group using Get-MgGroupOwner, then uses Get-MgUser to display their personal details like DisplayName, Mail, UserPrincipalName.

Best Practices for Using Get-MgUser

  • Limit Scope: Avoid fetching excessive data by using filters or specific parameters.
  • Simulate Actions: Use the -WhatIf parameter to preview commands without making changes.
  • Secure Connections: Always disconnect from Microsoft Graph after completing tasks.

Conclusion

The Get-MgUser cmdlet is an indispensable tool for Microsoft 365 administrators, offering powerful ways to automate user management. Whether retrieving all users, filtering based on attributes, or nesting it with other cmdlets for complex workflows, this cmdlet streamlines tasks and reduces manual effort.

By mastering Get-MgUser, you’ll not only save time but also unlock the full potential of Microsoft Graph PowerShell for managing users effectively. Ready to give it a try?


Permission Required

Example:


                            


                            


                            

© Your Site Name. All Rights Reserved. Design by HTML Codex