How to: create batch of test users in Office 365 with PowerShell

Tags: Office 365, PowerShell, How to

During testing Office 365, one often need to create several user accounts. Those user accounts can afterwards be assigned different permission levels to various Office 365 services, and used for testing across Office 365 services. It is possible to create those accounts using Office 365 tenant admin pages manually, or to import CSV file with user accounts using wizard. There is also third option - to use PowerShell. In our scenario, we need to create several accounts with as little effort as possible. All accounts should be unique, yet still easy recognizable. For creation of user accounts in Office 365 we are using `New-MsolUser` cmdlet which is a part of Azure Active Directory Module for Windows PowerShell. For `New-MsolUser` cmdlet, it is required that `-UserPrincipalName` and `-DisplayName` parameters are populated. All other parameters are optional. As part of user creation process, we can assign licence to an user - all with same cmdlet. All test users will have same password, and password expiration will be disabled. To accomplish all this, we can use parameters of the `New-MsolUser` cmdlet: `-Password ` To set password of the user we are going to create `-PasswordNeverExpires $true` To configure that password never expires for our test users `-LicenseAssignment ` Available licences for a tenant can be retrieved by executing Get-MsolAccountSku cmdlet We will also use "for" loop in PowerShell to execute New-MsolUser cmdlet for each new test user we need to create. Here is full sequence of PowerShell commands: Connect-MsolService Get-MsolAccountSku for($i=1; $i -le 10; $i++){ New-MsolUser -UserPrincipalName "user$" -Password "pass@word1" -PasswordNeverExpires $true -FirstName "User" -LastName $i -DisplayName "User $i" -UsageLocation "BA" -LicenseAssignment "LicenceNameRetrievedInPreviousCommand" } More info and downloads: - New-MsolUser [][1] - Manage Azure AD using Windows PowerShell [][2] - Azure Active Directory Module for Windows PowerShell (64-bit version) [][3] [1]: [2]: [3]:

No Comments

Add a Comment