Ultimate Guide for Using New-MgUser Cmdlet

Managing users is one of the most fundamental tasks for Microsoft 365 administrators. Whether onboarding new employees, managing existing accounts, or handling bulk user imports, a streamlined approach to creating user accounts is essential for efficiency and accuracy.

The New-MgUser cmdlet, part of the Microsoft Graph PowerShell module, allows administrators to automate and simplify the process of adding users to a0020Microsoft 365 environment. This guide walks you through every detail of using the cmdlet, with practical examples and best practices

Who Are Microsoft 365 Users?

Microsoft 365 users are accounts that grant individuals access to various Microsoft 365 services such as Exchange Online, Teams, SharePoint, and OneDrive. These accounts can be:

  • Employee Accounts: Regular users who need access to company resources.
  • Service Accounts: Non-human accounts used by applications or automation tasks.
  • Guest Accounts: External users with limited access to specific resources.

Admins are responsible for creating and managing these user accounts to ensure smooth access and security.

Why Use New-MgUser?

The New-MgUser cmdlet simplifies the process of creating user accounts by allowing admins to:

  • Automate user creation for consistency and accuracy.
  • Perform bulk user creation tasks, saving time and effort.
  • Manage complex scenarios such as assigning specific attributes or licenses during account creation.

While the Microsoft 365 Admin Center offers a graphical interface for creating users, New-MgUser is far more efficient for repetitive or large-scale tasks.

Setting Up Microsoft Graph PowerShell

Before you can use New-MgUser, you need to install and configure the Microsoft Graph PowerShell module:

  1. Install the Module: Run the following command to install the Microsoft Graph module:
    Install-Module Microsoft.Graph -Scope CurrentUser
  2. Connect to Microsoft Graph: Establish a connection to your Microsoft 365 environment with the required permissions:
    Connect-MgGraph -Scopes "User.Read.All"
  3. Disconnect After Use: To maintain security, disconnect your session once tasks are complete:
    Disconnect-MgGraph

Exploring the New-MgUser Cmdlet

The New-MgUser cmdlet is used to create new user accounts in Microsoft 365. It offers flexibility in assigning attributes like display name, user principal name (UPN), and job title during user creation.

Cmdlet Syntax

New-MgUser -DisplayName <String> -UserPrincipalName <String> -MailNickname <String> -PasswordProfile <PasswordProfile>  

The -PasswordProfile parameter is mandatory, as every new user account requires an initial password.

Practical Examples of New-MgUser

Add a Single User

To add a single user with basic details, use:

New-MgUser -DisplayName "John Doe" -UserPrincipalName "john.doe@contoso.com" -MailNickname "johndoe" -PasswordProfile @{password="P@ssw0rd!"; forceChangePasswordNextSignIn=$true}

This command creates a user named John Doe with an enabled account and a temporary password.

Add Multiple Users

For adding multiple users in one script, you can define user details in an array and loop through them:

$users = @(  
    @{ DisplayName="Jane Smith"; UserPrincipalName="jane.smith@contoso.com"; MailNickname="janesmith"; Password="P@ssw0rd1!" },  
    @{ DisplayName="Mark Taylor"; UserPrincipalName="mark.taylor@contoso.com"; MailNickname="marktaylor"; Password="P@ssw0rd2!" }  
)
foreach ($user in $users) {
    New-MgUser -DisplayName $user.DisplayName -UserPrincipalName $user.UserPrincipalName -MailNickname $user.MailNickname -PasswordProfile @{password=$user.Password; forceChangePasswordNextSignIn=$true}
}
                            

This script iterates through the array and creates multiple users.

Bulk User Import

To handle bulk user creation, import user details from a CSV file and use them in a script:

$users = Import-Csv -Path "C:\Users\admin\Documents\users.csv"
foreach ($user in $users) {
    New-MgUser -DisplayName $user.DisplayName -UserPrincipalName $user.UserPrincipalName -MailNickname $user.MailNickname -PasswordProfile @{password=$user.Password; forceChangePasswordNextSignIn=$true}
}
                            

This approach is ideal for onboarding large numbers of users.

Best Practices for Using New-MgUser

  • Validate Input Data: Always verify input data (e.g., email addresses, display names) to avoid errors during user creation.
  • Secure Passwords: Use secure methods to store and handle passwords, such as environment variables or encrypted files.
  • Test Scripts: Run scripts in a test environment before deploying them in production to ensure they work as expected.
  • Monitor User Creation: Log user creation activities to track errors or inconsistencies.

Conclusion

The New-MgUser cmdlet is a powerful tool for Microsoft 365 administrators, enabling efficient and accurate user creation. Whether you’re adding a single user, creating multiple accounts, or handling bulk imports, this cmdlet simplifies the process and saves time.

By mastering New-MgUser and following best practices, you can streamline user onboarding and ensure a smooth experience for your organization.

Did You Know? Managing Microsoft 365 applications is even easier with automation. Try our Graph PowerShell scripts to automate tasks like generating reports, cleaning up inactive Teams, or assigning licenses efficiently.

Ready to get the most out of Microsoft 365 tools? Explore our free Microsoft 365 administration tools to simplify your administrative tasks and boost productivity.

© M365Corner. All Rights Reserved.