Easily remove users from Office 365 using PowerShell

2020 06 17 16 11 47 Windows PowerShell

If you have a long list of users to remove from Office 365 using PowerShell is going to be the easiest way to achieve this with very little effort.

Installing MSOnline

First you are going to need to install the MSOnline PowerShell commands, this can be done using the PowerShell Gallery.

Launch PowerShell as an Administrator

2020 06 17 16 05 15 Inbox DRhodes@alamobs.co .uk Outlook

Now run the command Install-Module -Name MSOnline when asked to press Y to agree to the install

Connecting to MSOnline

Once installed close PowerShell and reopen. Now you have the prerequisites installed you can run the command Connect-MsolService to connect to MSOnline.

At this point you will be prompted with a login box, enter your tenant admin account details and click Next.

2020 06 17 16 09 36 Add a New Post ‹ The ICT Guy — WordPress and 3 more pages Work Microsoft​ Ed

Confirming you are connected to MSOnline

To confirm you are connected run the command Get-MsolUser which should return a list of all the users on the tenant.

Using Excel to create the commands to remove the users

Now you are ready to remove the accounts. To do this you can either use Excel to CONCATENATE the commands required as below

2020 06 17 16 16 13 Book1 Excel

Removing users using a CSV Import

Or if you have the users in a CSV file you can use the below script to import and remove each user from the CSV file. For the below script to work correctly the CSV must have a header title UPN with the users email address in the fields below

2020 06 17 16 21 50 Book1

I deleted users I didn’t want to delete, what can I do?

$Users = Import-CSV File.CSV
Foreach($User in $Users){
    Get-MSOLUser -UserPrincipalName $User.UPN | Remove-MSOLUser -Force

Obviously, with any delete function please take caution, and as always we take no responsibility if you delete your users by accident from using this guide. However, if you do you can recover them from the below screen for up to 30 days.

2020 06 17 16 23 18

Found priceless insights in this blog? Support the author’s creativity – buy them a coffee!

Leave a Reply

Your email address will not be published. Required fields are marked *