PowerShell Command to Add Users in Office 365
Install-Module -Name Microsoft.Graph
Import Module
Import-Module Microsoft.Graph
Connect to Microsoft Graph with necessary scopes
Connect-MgGraph -Scopes "User.ReadWrite.All", "Organization.Read.All"
Add Single User using PowerShell
To add Single User, copy and run the below command after changing the details and Password (as you want).
New-MgUser -DisplayName "James Parkar" `
-UserPrincipalName "example@domain.com" `
-MailNickName "james" `
-GivenName "James" `
-Surname "Parkar" `
-UsageLocation "IN" `
-PasswordProfile @{ Password = "Password@123"; ForceChangePasswordNextSignIn = $true } `
-AccountEnabled
Add Multiple users using PowerShell
# Import the CSV file
$users = Import-Csv -Path "Location of .csv file"
# Loop through each user in the CSV file
foreach ($user in $users) {
# Create a new user
$newUser = New-MgUser -UserPrincipalName $user.UserPrincipalName `
-DisplayName $user.DisplayName `
-GivenName $user.FirstName `
-Surname $user.LastName `
-JobTitle $user.JobTitle `
-Department $user.Department `
-AccountEnabled `
-MailNickname ($user.UserPrincipalName.Split('@')[0]) `
-PasswordProfile @{
ForceChangePasswordNextSignIn = $true
Password = "TempPassword123!"
}
# Set UsageLocation (required for license assignment)
Update-MgUser -UserId $newUser.Id -UsageLocation "IN"
}
That's it.
Read More: How to connect and access user in PowerShell?
Read More: Import MBOX to Gmail
