top of page

PowerShell Script To Remove Office 365 Users From CSV Input


In this blog, we will learn how to write a PowerShell script which will read principal user name from the CSV file and remove those users from Office 365 account.

Step 1

a. Connect Windows Azure Active Directory Module for Windows PowerShell.

b. Use the following code snippet to connect with Office 365 tenant.

$UserCredential = Get-Credential      

Connect-AzureAD -Credential $UserCredential    

Step 2

a. Execute the following script.

b. Below is a screenshot for my CSV file.

$sInputFile = "D:\deleteuser.csv"

$sColumnName = "UserPrincipalName" $tblDatos = Import - CSV $sInputFile  foreach($fila in $tblDatos)   


"Deleting user " + $fila.$sColumnName.ToString()      

Write - Host - ForegroundColor Yellow "User staring to deleter"     


Remove - MsolUser - UserPrincipalName 


Write - Host - ForegroundColor Yellow 

$fila.$sColumnName "Deleted........"     




c. It will ask for confirmation for deleting every user.

d. Press "Yes".


This is how we can use PowerShell script to delete multiple users from Office 365.



bottom of page