How To Create Multiple Office 365 Users With PowerShell

In this blog post,  I will show you how to create multiple Office 365 users with PowerShell and a CSV file.

Recently, I have shown you how to create an Office 365 user with PowerShell and now it is time to move to the next level and show and deal with a use case where you need to create multiple users.

The code that will create users is using a CSV file (shown below) that set a password however the last line will in code forces the user to change his password when her first log on to Office 365.

CSV

user,displayname,upn,nickname,password
NTTest01,NT Test 01,nttest01@domain.local,nttest,password
NTTest02,NT Test 02,nttest02@domain.local,nttest,password

Once you fill in the CSV file, Install and connect to Azure AD using the following cmdlet.

install-module azuread
connect-azuread

Code

After connecting, run the code below which will create the accounts.

$users = import-csv 0365_users.csv

foreach ($user in $users)
{
$newpassword = convertto-securestring $user.password -AsPlainText -Force 
$PasswordProfile = New-Object -TypeName Microsoft.Open.AzureAD.Model.PasswordProfile
$PasswordProfile.Password = $user.password
New-AzureADUser -DisplayName $user.displayname -PasswordProfile $PasswordProfile -UserPrincipalName $user.upn -AccountEnabled $true -MailNickName $user.nickname -verbose
set-azureaduserpassword -objectid $user.upn -password $newpassword -ForceChangePasswordNextLogin $true -verbose
}

Processing…
Success! You're on the list.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.