OpeningClouds
Adding Users to Office 365 via Powershell
Updated: Feb 8, 2019
Third installment in our Office 365 series on user management. This time around we will be adding users to your Office 365 Tenant via Powershell.
Powershell is Microsoft's .NET based command shell that has become their standard way for most automation and administrative tasks.
Windows 10 by default comes with Powershell but it is missing the elements needed to administer Office 365. To remedy this we have to install the updated modules.
From your Start menu you can manually look for Powershell or use type-ahead (Cortana) to find Powershell

Once you found Powershell, do NOT left-click on the icon. Instead, right-click on the icon and left-click on Run as administrator. Installing the Powershell module for Office 365 requires that the Powershell session you are running as Admin.

You will get the User Access Control confirming you want to run Powershell as Admin.

Click Yes when this pops up. Now your in Powershell. Powershell looks a lot like the old command prompt and you can run any command prompt command from Powershell. It really should become your standard Command Line Interface (CLI)
At the Powershell prompt type the following:
Install-Module MSOnline
Install-Module AzureAD
When you do you will be asked in Powershell if you want to download modules from an untrusted source. Truth be told, its a Microsoft owned and operated source but your computer does not now that. You can add it as a source or just type "Y" at the prompt.


In the future you can enter the following in Powershell and never see this message again.
Set-PSRepository PSGallery

Back in Powershell you will see it working in the background. Occasionally a Green status bar shoots across the screen.

Once the install is completed Powershell will go back to the prompt. To make sure the module installed you can run the following command:
Get-Module -ListAvaliable | Format-List Name, Description.
NOTE, In General, if a module install fails it usually tells you and why it failed.

Depending on how many Modules you have installed, scroll up and look for MSOnline as seen above. Each of these modules are made up of many other commands that help you manage your Tenant. To get a list of commands available, type:
Get-Command -Module MSOnline.
This will dump out every command for this module.

We are going to concentrate on the following commands.
For Users : Get-MsolUser, Set-MsoLUser, New-MsoLUser
You have to now connect to your tenant to manage it. To do so, type:
Connect-MsolService

This will Open Dialog boxes where you will enter your Portal Admin User account and Password
Sign-on Screen....

Enter admin account....

Enter Admin account password

Connected.

Now lets actually create a user.
In the Powershell type New-MSoLUser -userprincipalname 'PERSONSNAME@domain.com' -FirstName 'Users First Name' -LastName 'Users Last Name' -DisplayName 'How you want users name displayed'.

There are other fields you can enter, use this LINK as a reference.

Once created you can verify in the Portal that the user has been created.

Now that we can create users lets talk about updating users. The second command we are going to play with is the SET-MsolUser command. You use this to set other attributes on accounts that you did not set, or you can use it to change attributes of the accounts that already exist. In the next couple examples we are going to update a user accounts Title and then use both commands to update City for all our users.
To set an attribute we will use the following:
Set-MsolUser -userprincipalname 'USERNAME@DOMAIN' -Title 'Title'
For our example we are going to give Lois Lane the title of "Reporter"

Refresh the user in the portal again and you see it has been updated.

Now we can combine both commands. Powershell allows you to take the output of one command and pass it as an argument to another command. In this example we are going to use the Get-MsollUser and Set-MsolUser to set the 'City' attribute. Using a | (pipe) operator we can use do this. The following command will do this for us
Get-MsolUser | Set-MsolUser -City 'Gotham City'

Using the Get command again we can get users by attributes as well. We can again use one command, pipe it to anther and get the results we want.
get-msoluser | where-object {$_.City -like 'Gotham City'}

The where-object command lets you select attributes from your accounts. You have to set the attribute as a variable in the script like this. $_.ATTRIBUTE where ATTRIBUTE can by any property of the users account.
Our post ends here but I hope you can see some of the options you have here. Powershell and Office 365 are very powerful together.