PowerShell · Microsoft 365
Bulk-create Microsoft 365 users from CSV
Requires the Microsoft Graph PowerShell SDK. Prepare a CSV with columns: DisplayName, GivenName, Surname, UserPrincipalName, Department, JobTitle. Each user gets a random initial password that must be changed at first sign-in. Review the output log after each run to confirm all accounts were created successfully.
Script
Code
Import-Module Microsoft.Graph.Users
Connect-MgGraph -Scopes "User.ReadWrite.All"
$csvPath = ".
ew-hires.csv"
$users = Import-Csv -Path $csvPath
$results = @()
foreach ($row in $users) {
$passwordProfile = @{
Password = [System.Web.Security.Membership]::GeneratePassword(16, 3)
ForceChangePasswordNextSignIn = $true
}
$params = @{
DisplayName = $row.DisplayName
GivenName = $row.GivenName
Surname = $row.Surname
UserPrincipalName = $row.UserPrincipalName
MailNickname = ($row.UserPrincipalName -split "@")[0]
Department = $row.Department
JobTitle = $row.JobTitle
AccountEnabled = $true
PasswordProfile = $passwordProfile
UsageLocation = "US"
}
try {
$newUser = New-MgUser -BodyParameter $params
Write-Output "Created: $($newUser.UserPrincipalName)"
$results += [pscustomobject]@{ UPN = $newUser.UserPrincipalName; Status = "Created" }
} catch {
Write-Warning "Failed: $($row.UserPrincipalName) — $($_.Exception.Message)"
$results += [pscustomobject]@{ UPN = $row.UserPrincipalName; Status = "Failed" }
}
}
$results | Export-Csv -Path ".provision-results.csv" -NoTypeInformation
Write-Output "Done. See provision-results.csv for details."Usage
How to use this script
Requires the Microsoft Graph PowerShell SDK. Prepare a CSV with columns: DisplayName, GivenName, Surname, UserPrincipalName, Department, JobTitle. Each user gets a random initial password that must be changed at first sign-in. Review the output log after each run to confirm all accounts were created successfully.
Review the script and test in a non-production environment before running at scale.
Tags
microsoft-365user-provisioningonboarding
More scripts