PowerShell Script To Remove Office 365 Users From CSV Input

Overview

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"     

$fila.$sColumnName      

Remove - MsolUser - UserPrincipalName 

$fila.$sColumnName      

Write - Host - ForegroundColor Yellow 

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

$fila.     

 $sColumnName  

 


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


d. Press "Yes".


Conclusion

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

Recent Posts

See All