Create user accounts with Office 365 PowerShell

 

Topic Last Modified: 2017-05-17

Learn how to use Office 365 PowerShell to create user accounts in Office 365.

You can use Office 365 PowerShell to efficiently create user accounts, especially multiple user accounts. When you create user accounts in Office 365 PowerShell, certain account properties are always required. Other properties aren't required to create the account, but are otherwise important. These properties are described in the following table:

 

Property name Required? Description

DisplayName

Yes

This is the display name that's used in Office 365 services. For example, Caleb Sills.

UserPrincipalName

Yes

This is the account name that's used to sign in to Office 365 services. For example, CalebS@contoso.onmicrosoft.com.

FirstName

No

 

LastName

No

 

LicenseAssignment

No

This is the licensing plan (also known as the license plan, Office 365 plan, or SKU) from which an available license is assigned to the user account. The license defines the Office 365 services that are available to account. You don't have to assign a license to a user when you create the account, but the account requires a license to access Office 365 services. You have 30 days to license the user account after you create it.

Use the Get-MsolAccountSku cmdlet to view the licensing plans (AccountSkuId) and available licenses in your organization. For more information, see View licenses and services with Office 365 PowerShell.

Password

No

If you don't specify a password, a random password is assigned to the user account, and the password is visible in the results of the command. If you specify a password, it needs to meet the following complexity requirements:

  • 8 to 16 ASCII text characters.

  • Characters from any three of the following types: lowercase letters, uppercase letters, numbers, and symbols.

UsageLocation

No

This is a valid ISO 3166-1 alpha-2 country code. For example, US for the United States, and FR for France. It's important to provide this value, because some Office 365 services aren't available in certain countries, so you can't assign a license to a user account unless the account has this value configured. For more information, see About license restrictions.

The procedures in this topic require you to connect to Office 365 PowerShell. For instructions, see Connect to Office 365 PowerShell.

To create an individual account, use the following syntax:

New-MsolUser -DisplayName <DisplayName> -FirstName <FirstName> -LastName <LastName> -UserPrincipalName <Account> -UsageLocation <CountryCode> -LicenseAssignment <AccountSkuID> [-Password <Password>]

This example creates an account for the United States user named Caleb Sills, and assigns a license from the contoso:ENTERPRISEPACK (Office 365 Enterprise E3) licensing plan.

New-MsolUser -DisplayName "Caleb Sills" -FirstName Caleb -LastName Sills -UserPrincipalName calebs@contoso.onmicrosoft.com -UsageLocation US -LicenseAssignment contoso:ENTERPRISEPACK

  1. Create a comma-separated value (CSV) file that contains the required user account information. For example:

    UserPrincipalName,FirstName,LastName,DisplayName,UsageLocation,AccountSkuId
    ClaudeL@contoso.onmicrosoft.com,Claude,Loiselle,Claude Loiselle,US,contoso:ENTERPRISEPACK
    LynneB@contoso.onmicrosoft.com,Lynne,Baxter,Lynne Baxter,US,contoso:ENTERPRISEPACK
    ShawnM@contoso.onmicrosoft.com,Shawn,Melendez,Shawn Melendez,US,contoso:ENTERPRISEPACK
    

    Note:   The column names and their order in the first row of the CSV file are arbitrary, but make sure the data in the rest of the file matches the order of the column names, and use the column names for the parameter values in the Office 365 PowerShell command.

  2. Use the following syntax:

    Import-Csv -Path <InputCSVFile> | foreach {New-MsolUser -DisplayName $_.DisplayName -FirstName $_.FirstName -LastName $_.LastName -UserPrincipalName $_.UserPrincipalName -UsageLocation $_.UsageLocation -LicenseAssignment $_.AccountSkuId [-Password $_.Password]} | Export-Csv -Path <OutputCSVFile>
    

    This example creates the user accounts from the file named C:\My Documents\NewAccounts.csv, and logs the results in the file named C:\My Documents\NewAccountResults.csv

    Import-Csv -Path "C:\My Documents\NewAccounts.csv" | foreach {New-MsolUser -DisplayName $_.DisplayName -FirstName $_.FirstName -LastName $_.LastName -UserPrincipalName $_.UserPrincipalName -UsageLocation $_.UsageLocation -LicenseAssignment $_.AccountSkuId} | Export-Csv -Path "C:\My Documents\NewAccountResults.csv"
    
  3. Review the output file to see the results. We didn't specify passwords, so the random passwords that were generated are visible in the output file.

To use the New-AzureADUser cmdlet from the Azure Active Directory V2 PowerShell module, you must first connect to your subscription. For the instructions, see Connect with the Azure Active Directory V2 PowerShell module.

After you have connected, use the following syntax to create an individual account:

$PasswordProfile=New-Object -TypeName Microsoft.Open.AzureAD.Model.PasswordProfile
$PasswordProfile.Password="<user account password>"
New-AzureADUser -DisplayName <DisplayName> -GivenName <FirstName> -SurName <LastName> -UserPrincipalName <Account> -UsageLocation <CountryCode> -MailNickName <mailbox name> -PasswordProfile $PasswordProfile -AccountEnabled $true

This example creates an account for the United States user named Caleb Sills:

$PasswordProfile=New-Object -TypeName Microsoft.Open.AzureAD.Model.PasswordProfile
$PasswordProfile.Password="3Rv0y1q39/chsy"
New-AzureADUser -DisplayName "Caleb Sills" -GivenName "Caleb" -SurName "Sills" -UserPrincipalName calebs@contoso.onmicrosoft.com -UsageLocation US -MailNickName calebs -PasswordProfile $PasswordProfile -AccountEnabled $true
NoteNote:
At the time of the publication of this article, you must assign licenses to the new account using the Office Admin center.
 
Show: