New-MgUser cmdlet to add over 500 external users?

David M Tressler 20 Reputation points
2025-12-03T14:08:18.2266667+00:00

Can you create a powershell script that runs the New-MgUser cmdlet to add over 500 external users?

Microsoft Security | Microsoft Entra | Microsoft Entra ID

Answer accepted by question author

AI answer

2025-12-03T14:08:29.3166667+00:00

To add over 500 external users using the New-MgUser cmdlet in PowerShell, you can create a script that loops through a list of user details and creates each user. Below is an example script that demonstrates how to achieve this:

# Define the list of external users to add
$users = @(
    @{DisplayName='User1'; MailNickName='User1'; UserPrincipalName='******@external.com'; Password='Password1!'},
    @{DisplayName='User2'; MailNickName='User2'; UserPrincipalName='******@external.com'; Password='Password2!'},
    # Add more users as needed
)

# Loop through each user and create them
foreach ($user in $users) {
    $PasswordProfile = @{ Password = $user.Password }
    New-MgUser -DisplayName $user.DisplayName -PasswordProfile $PasswordProfile -AccountEnabled $true -MailNickName $user.MailNickName -UserPrincipalName $user.UserPrincipalName
}

Notes:

  • Ensure that you have the necessary permissions to create users, such as User.ReadWrite.All.
  • The Password should meet your organization's password policy requirements.
  • You can expand the $users array to include all 500+ users you wish to add.

This script will create each user in the loop by calling the New-MgUser cmdlet with the specified parameters.


References:

AI-generated content may be incorrect. Read our transparency notes for more information.

Was this answer helpful?

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.