Guides Creating Users in Office 365 the PowerShell Way

Creating Users in Office 365 the PowerShell Way

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 [email protected] -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 [email protected] 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 [email protected] -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
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 [email protected], 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 [email protected].

Follow ServerWatch on Twitter and on Facebook

Latest Posts

How to Convert a Physical Computer to a Virtual Machine

Many organizations are implementing virtualization technology into their networks to convert physical computers to virtual machines (VM). This helps reduce overall physical hardware costs,...

HPE ProLiant DL380 Gen10: Rack Server Overview and Insight

The HPE ProLiant DL380 series has consistently been a market leader in the server space. The Gen10 released in 2017 further increased HPE's market...

Best Server Management Software & Tools 2021

Finding the best server management software tools for your organization can have a major impact on the success of your business operations. Manually handling...

IBM AS/400: Lasting the Test of Time

Some server operating systems (OS) were built to survive the test of time – the IBM AS/400 is one such system.  The AS/400 (Application System/400)...

What is Disaster Recovery?

The modern organization's heavy dependence on using data to drive their business has made having a Disaster Recovery (DR) plan in place a necessity....

Related Stories