GuidesCreating Users in Office 365 the PowerShell Way

Creating Users in Office 365 the PowerShell Way

ServerWatch content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.




PowerShell is a great way to interact with Office 365 resources. When you subscribe to Office 365, you are given a free Windows Azure Active Directory, or WAAD for short. WAAD maintains the users and groups you create using Office 365 Admin Center or the users that are synced from the On-Premises Active Directory. Windows Server Tutorials

In addition to being able to create users using Office Admin Center, you can also use PowerShell cmdlets to create them.

Before working with Office 365 PowerShell cmdlets, you’ll need to download and install Microsoft Online Sign-In Assistance for IT Professional and the Windows Azure Active Directory Module for Windows PowerShell on a computer running Windows 7 or later operating system.

After installing the Windows Azure Active Directory Module for Windows PowerShell, go to start > search > and type “Windows Azure Active Directory Module,” and then right-click on the shortcut and next click “Run As Administrator” to open Azure PowerShell window in an elevated mode. Before using any Office 365 PowerShell cmdlets, connect to your Office 365 tenant by issuing the Connect-MsolService command.

To create Office 365 users via the command line, you can use the New-MsolUsercmdlet. The New-MsolUser cmdlet supports creating a single or multiple users by reading a CSV file.

The CSV file holds the information about the users such as user principal name, display name, city, phone number, street address, etc. Before you can create a single or multiple users, you need to know which of the user properties are mandatory. You can obtain the information about mandatory properties by creating a new user as shown in the command below:

New-MsolUser -UserPrincipalName kio@serverwatch.com -DisplayName "Kio Tim"

The output of the command is shown below:

Windows Azure Active Directory PowerShell

As you can see in the output above, after executing the command a new user is provisioned in the Office 365 WAAD named Kio@ServerWatch.com. The only required properties when creating a user in this case are “UserPrincipalName” and “DisplayName.” Since a user that is provisioned in Office 365 must also have a password associated with it, the above command generates a random password for the user.

So at this stage, you know the mandatory properties needed to create a user in Office 365 WAAD using the New-MsolUSer cmdlet. The only required properties are UserPrincipalName and DisplayName, but pay attention to the output shown in the screenshot above.

The New-MsolUser cmdlet also generates a random password for the user being created and the password is shown in the PowerShell window. When creating thousands of Office 365 users, this approach will likely be unfeasible. With the password of the user randomly generated, it will be difficult for you to capture the password for each user when the output is shown in a PowerShell window. To overcome this problem, what you can do is use the “Export-CSV” command in a pipe operator as shown in the following command:

New-MsolUser -UserPrincipalName kio@ServerWatch.com -DisplayName "Kio Tim" | Export-CSV C:TempNewOffice365UsersWithPassword.CSV -NoTypeInformation

The above command creates a new user and the output of the command is exported to a CSV file along with the randomly generated password for the user. The same approach can be used when creating bulk users in Office 365 WAAD.

You don’t really need to write a bunch of statements in a PowerShell script before you can create multiple users. All you need is a CSV file that holds the user information and then a single command or two as we’ll cover shortly that processes the file for creating bulk users. The resulting CSV file should look like this:

Windows Azure Active Directory PowerShell Cmdlet

As shown in the CSV file above, it also has the password column. You can use the password column from the CSV file to set the password for each user, if required.

Case A – To create users from the CSV file with a random password generated by the New-MsolUser cmdlet and store the output in a CSV file, use the following PowerShell commands:

$AllOffice365Users = Import-CSV C:TempUserFile.CSV
$AllOffice365Users | ForEach-Object {New-MsolUser -UserPrincipalName $_.UserPrincipalName -DisplayName $_.DisplayName} | Export-CSV C:TempNewOffice365UsersWith_RandomPassword.CSV -NoTypeInformation

Case B – To create users from the CSV file and set the password for each user from the CSV file then you’ll use these PowerShell commands:

$AllOffice365Users = Import-CSV C:TempUserFile.CSV
$AllOffice365Users | ForEach-Object {New-MsolUser -UserPrincipalName $_.UserPrincipalName -DisplayName $_.DisplayName -Password $_.UserPassword} | Export-CSV C:TempNewOffice365UsersWithPassword.CSV -NoTypeInformation

Tip: With the password for each user set from the CSV file in Case B, you may or may not want to use the “Export-CSV” command.

If you wish users to change their password at the next logon, add the “-ForceChangePassword” switch and set it to “True” as shown below:

$AllOffice365Users = Import-CSV C:TempUserFile.CSV
$AllOffice365Users | ForEach-Object {New-MsolUser -UserPrincipalName $_.UserPrincipalName -DisplayName $_.DisplayName} -ForceChangePassword True | Export-CSV C:TempNewOffice365UsersWith_RandomPassword.CSV -NoTypeInformation

This is the process for creating bulk users from a CSV file that already has the information about the users. Now if you have subscribed to Office 365 and would like to create 5000 or more users in Office 365 WAAD for testing purposes, you can create a CSV file that has the user information using a CMD script as listed below:

@Echo OFF
SETLocal EnableDelayedExpansion
CLS
SET LN=1
DEL TestUsers.CSV
Echo UserPrincipalName,DisplayName> UserFile.CSV
FOR /L %%G IN (1,1,5000) DO (
SET /a LN=LN+1
SET TT = User!LN!
Echo !TT!
Echo User!LN!@serverwatch.com, DisplayName_User!LN!>> UserFile.CSV
)

And then use the New-MsolUser command, as shown below, to create users from the CSV file that was created using the earlier script:

$AllOffice365Users = Import-CSV C:TempUserFile.CSV
$AllOffice365Users | ForEach-Object {New-MsolUser -UserPrincipalName $_.UserPrincipalName -DisplayName $_.DisplayName} -ForceChangePassword True | Export-CSV C:TempNewOffice365UsersWith_RandomPassword.CSV -NoTypeInformation


Nirmal Sharma is a MCSEx3, MCITP and Microsoft MVP in Directory Services. He specializes in directory services, Microsoft Azure, Failover clusters, Hyper-V, System Center and Exchange Servers, and has been involved with Microsoft technologies since 1994. In his spare time, he likes to help others and share some of his knowledge by writing tips and articles on various sites and contributing to Health Packs for ADHealthProf.ITDynamicPacks.Net solutions. Nirmal can be reached at nirmal_sharma@mvps.org.

Follow ServerWatch on Twitter and on Facebook

Get the Free Newsletter!

Subscribe to Daily Tech Insider for top news, trends & analysis

Latest Posts

Related Stories