How to create Office 365 user accounts, assign licenses and enable MFA using PowerShell with steps for creating multiple users in bulk by importing from csv file
References:
Create Microsoft 365 user accounts with PowerShell
https://docs.microsoft.com/en-us/microsoft-365/enterprise/create-user-accounts-with-microsoft-365-powershellEnable per-user Azure AD Multi-Factor Authentication
by Author
https://docs.microsoft.com/en-us/azure/active-directory/authentication/howto-mfa-userstates
Connect to Azure Active Directory PowerShell
Connect-MsolService
Run Get-MsolAccountSku to find the Office 365 license sku to use for assigning licenses
Get-MsolAccountSku
| Product Name | SKU |
| Microsoft 365 E3 | SPE_E3 |
| Microsoft 365 Business Premium | SPB |
| Office 365 Enterprise E3 | ENTERPRISEPACK |
Create a new user account using the new-msoluser PowerShell command
New-MsolUser options
Country code examples:
AU Australia
DE Germany
FR France
GB United Kingdom
US United States
You can get a complete list of country codes from MaxMind
GeoIP Legacy Codes
https://dev.maxmind.com/geoip/legacy/codes
New-MsolUser -DisplayName "Philip Fry" -FirstName Philip -LastName Fry -UserPrincipalName philip@domain.com -UsageLocation GB -LicenseAssignment techlabscloud:ENTERPRISEPACK -Password PASSWORD
We will create a StrongAuthenticationRequirement variable $sta, this will then be used to enable MFA for the user by running the Set-MsolUser -StrongAuthenticationRequirements PowerShell command
# Create a StrongAuthenticationRequirement object $st = New-Object -TypeName Microsoft.Online.Administration.StrongAuthenticationRequirement $st.RelyingParty = "*" $st.State = "Enabled" $sta = @($st) # Enable MFA for the user Set-MsolUser -UserPrincipalName philip@domain.com -StrongAuthenticationRequirements $sta
MFA is now enabled
Create multiple user accounts using New-MsolUser and Import-Csv
Create a csv file list of new users using these column headings
UserPrincipalName, FirstName, LastName, DisplayName, UsageLocation, AccountSkuId
Run the MsolUser PowerShell command with Import-Csv to create Office 365 users in bulk from the csv file
• The output file contains results of the PowerShell command including a randomly generated password for each user
• You could also specify an initial password for the user accounts in the csv file by adding a column for Password and adding the option -Password to the PowerShell command
Import-Csv -Path C:\Temp\planet-express-users.csv | foreach {New-MsolUser -DisplayName $_.DisplayName -FirstName $_.FirstName -LastName $_.LastName -UserPrincipalName $_.UserPrincipalName -UsageLocation $_.UsageLocation -LicenseAssignment $_.AccountSkuId -Password $_.Password} | Export-Csv -Path C:\temp\created-users.csv
User accounts have been created and licenses assigned
Enable MFA for multiple users using Set-MsolUser and Import-Csv
Let's use the same csv list that we used to create the user accounts to enable MFA for all users on the list
$users = Import-Csv -Path C:\Temp\planet-express-users.csv
foreach ($user in $users)
{
$st = New-Object -TypeName Microsoft.Online.Administration.StrongAuthenticationRequirement
$st.RelyingParty = "*"
$st.State = "Enabled"
$sta = @($st)
Set-MsolUser -UserPrincipalName $user.UserPrincipalName -StrongAuthenticationRequirements $sta
}
All user accounts from the csv list now have MFA enabled
Comments