Creating User Accounts in Microsoft 365 using PowerShell


When it comes to managing accounts in M365, why not take a look at a few simple commands which can help you create mulitple accounts very quickly and effectively.

Load up PowerShell on a device which has Internet access as an administrator.

We’ll to import the MSOnline module. To do this issue the following command:

Find-Module -Name MSOnline | Install-Module -Force  

This will download and install the module.

Now we need to connect to our M365 subscription but first we will store our user credentials in a variable. To do this issue the following command:

$MSOnlineCred = Get-Credential

When you execute the command you’ll be prompted to enter username/password credentials. Enter the details of a user who has rights to manage users in your M365 subscription.

If you query the variable you will see the details of the credentials are stored.

With the credentials stored, we can connect to the subscription using the Connect-MsolService cmdlet.

Connect-MsolService -Credential $MSOnlineCred

The Get-MsolUser cmdlet provides us with a way to get all the users in our subscription. The returned information provides me with the UserPrincipalName, DisplayName and isLicensed details.

From the details below, I can see one of the accounts has a licenced assigned.

To view the information about your current licencing plans and the available licences for each plan, run the following command:

Get-MsolAccountSku

When running this cmdlet, I can see I have some EMS licences which are available for consumption.

So now let’s create a user account and assign one of those licences to the user. The New-MSolUser cmdlet is used to create accounts.

I’m going to specify the UserPrinicpalName, DisplayName, FirstName, LastName, UsageLocation and LicenseAssignment.

So what is the UsageLocation? Well this is the country code and M365 uses the Alpha-2-code listed at iso.org. For a full list of the codes check out https://www.iso.org/obp/ui/#search and check the Alpha-2-code.

The code to create my new user with the EMS licence assigned is as below:

New-MsolUser -UserPrincipalName "PS.User@sccmsolutions.co.uk" -DisplayName "PS User" -FirstName "Ps" -LastName "User" -UsageLocation "GB" -LicenseAssignment sccmsolution:EMSPREMIUM

The user will be created. Note that the password will be generated and displayed in the results.

Go to the Microsoft 365 admin center at admin.microsoft.com and navigate to Users\Active Users and you’ll see the new account listed.

To remove the user account from M365 issue the Remove-MSolUser command:

Remove-MsolUser -UserPrincipalName "PS.User@sccmsolutions.co.uk" -Force

This will remove the user and any associated licenced will be freed up and added back to the pool of licences for consumption by another user.

Hope this helps you kick start you on your journey to user management of M365 via PowerShell

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s