I have read many confusing and convoluted ways to remove users and groups from AD. For example, I demo Azure a lot and have a lot of users left behind and removing them one by one in the portal would take me ages, look:

So the easiest way is by using PowerShell.

You need to download the following two components in order to be able to connect to our Azure AD:

Once installed open the Azure Active Directory Module for Windows PowerShell:

Now we need to connect to our Azure AD. Type the following command Connect-MSOLService and you will be prompted for your credentials.

Ensure you use an account with Global Admin rights in Azure AD otherwise you will end up with errors like below

Connect-MsolService : Exception of type
‘Microsoft.Online.Administration.Automation.MicrosoftOnlineException’ was thrown.
At line:1 char:1

+ Connect-MsolService
+ ~~~~~~~~~~~~~~~~~~~

+ CategoryInfo : OperationStopped: (:) [Connect-MsolService], MicrosoftOnlineException
+ FullyQualifiedErrorId : 0x800488D6,Microsoft.Online.Administration.Automation.ConnectMsolService

You now need to export all the user accounts to CSV file. Type the following command:

Get-MsolUser –All | Export-CSV c:\users.csv

Edit your CSV and remove any accounts you do not want to delete (ie, your account and other Global Administrative accounts!)

Save the file.

So let’s now remove those users by using the following command:

Import-CSV c:\users.csv | Remove-MsolUser –Force

Once complete (it may take some time so be patient) you will see the accounts have gone in the portal (with the exception of the Global Admin accounts I wanted to keep):

Now we can do the same for Groups. Type the following command:

Get-MsolGroup –All | Export-CSV c:\groups.csv

Edit your CSV and remove any groups you do not want to delete.

Save the file.

So let’s now remove those users by using the following command:

Import-CSV c:\groups.csv | Remove-MsolGroup –Force

Once complete (it may take some time so be patient) you will see the accounts have gone in the portal:

Simple.