Managing user accounts is a critical task in Microsoft 365 administration. Pairing Get-MgUser and New-MgUser allows administrators to create new users and immediately verify their details. This article demonstrates a simple use case with practical tips for everyday user management.
The New-MgUser cmdlet enables you to create new users in Microsoft 365, while Get-MgUser retrieves user details, such as their display name, email, and assigned licenses. Using these cmdlets together ensures accuracy during the user creation process by allowing immediate verification.
# Step 1: Define new user details
$newUser = @{
DisplayName = "Jane Doe"
UserPrincipalName = "jane.doe@domain.com"
MailNickname = "janedoe"
AccountEnabled = $true
PasswordProfile = @{
ForceChangePasswordNextSignIn = $true
Password = "P@ssw0rd123!"
}
}
# Step 2: Create the user
try {
New-MgUser -BodyParameter $newUser
Write-Output "User created successfully: $($newUser.DisplayName)"
} catch {
Write-Error "Failed to create user: $_"
return
}
# Step 3: Verify the user's details
$user = Get-MgUser -Filter "userPrincipalName eq 'jane.doe@domain.com'"
if ($user -ne $null) {
Write-Output "User Found: $($user.DisplayName)"
Write-Output "User Principal Name: $($user.UserPrincipalName)"
Write-Output "Account Enabled: $($user.AccountEnabled)"
} else {
Write-Error "User verification failed: User not found."
}
Get-MgUser with the -Filter parameter to check if the user already exists before creating them:
$existingUser = Get-MgUser -Filter "userPrincipalName eq 'jane.doe@domain.com'"
if ($existingUser) {
Write-Error "User already exists."
return
}
PasswordProfile to meet organizational security requirements.AccountEnabled to $true for active users or $false for inactive accounts at the time of creation.DisplayName, UserPrincipalName, and MailNickname are provided.
$users = Import-Csv "users.csv"
foreach ($user in $users) {
$newUser = @{
DisplayName = $user.DisplayName
UserPrincipalName = $user.UserPrincipalName
MailNickname = $user.MailNickname
AccountEnabled = $true
PasswordProfile = @{
ForceChangePasswordNextSignIn = $true
Password = $user.Password
}
}
New-MgUser -BodyParameter $newUser
}
| Error Message | Cause | Solution |
| User Already Exists | Attempting to create a user with an existing UPN | Check for existing users with Get-MgUser before creating new ones. |
| Invalid Password | Password does not meet complexity requirements | Ensure passwords comply with the organization's password policy. |
| Insufficient Permissions | Missing required permissions | Grant User.ReadWrite.All or Directory.ReadWrite.All permissions. |
| Invalid Property Value | Missing or incorrect user properties | Verify that all required properties are correctly specified. |
Pairing Get-MgUser and New-MgUser simplifies the process of creating and verifying user accounts in Microsoft 365. This approach ensures that user accounts are created accurately and allows for immediate validation to avoid potential issues.
By incorporating these cmdlets into your workflows, you can streamline user management and enhance administrative efficiency in your organization.
© m365corner.com. All Rights Reserved. Design by HTML Codex