Bulk Removing Azure Active Directory Users using PowerShell

Posted by Anuraj on Saturday, March 10, 2018

Azure PowerShell

This post is about deleting Azure Active directory. Sometimes you can’t remove your Azure Active Directory, because of the users and / or applications created or synced on it. So you can’t remove the users from Azure Portal.

So first you need to download the Azure AD from PowerShell gallery. You can download it from here.

Once it downloaded, you need to connect to Azure Active Directory, you can do this using Connect-AzureAD command. You need to provide the tenant id as the parameter. You can get the tenant id from Azure active directory properties and then take the directory id value.

Azure Active Directory - Tenant Id

Once you execute the command, PowerShell will prompt you with windows login dialog, once successfully logged in, it will display the details of the active directory tenant.

Connect-AzureAD command response

Next you need to download all the users from the tenant, you can do this with following command.

Get-AzureADUser -All $true | Export-Csv D:\ADSample\AllUsers.csv

This command will download all the users from Azure Active directory and export it as CSV file. Once you done it, you can execute the following command, which will delete the all the users based on the csv file.

Import-CSV D:\ADSample\AllUsers.csv | Foreach-Object { Remove-AzureADUser -ObjectId $_.UserPrincipalName }

Once the all users deleted, you can delete the active directory.

Happy Programming :)

Similar Posts

Did you like this article? Share it with your friends

Facebook Twitter Google+ LinkedIn Reddit StumbleUpon

BMC logoBuy me a coffee

Copyright © 2018 - Anuraj P. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Hosted with ❤ by GitHub