Deleting bulk user accounts made easy using Powershell.
We have tested the below script in Windows Server 2008 R2 AD infrastructure.
Users are present in Sales OU as shown below.

Create a txt or csv file with user SamAccountName as shown below.

Below is the Powershell Script to delete the bulk users from AD.

The Script data is below, copy the below text into Notepad, and save it as RemoveAccount.ps1
Import-Csv .\Remove.txt | foreach-object {
remove-aduser -identity $_.SamAccountName -confirm:$false }

Accounts are deleted as shown below.

We have tested the below script in Windows Server 2008 R2 AD infrastructure.
Users are present in Sales OU as shown below.

Create a txt or csv file with user SamAccountName as shown below.

Below is the Powershell Script to delete the bulk users from AD.

The Script data is below, copy the below text into Notepad, and save it as RemoveAccount.ps1
Import-Csv .\Remove.txt | foreach-object {
remove-aduser -identity $_.SamAccountName -confirm:$false }
To run the script
on AD:
- Run PowerShell as Administrator
- Change to the location where you have placed the Script and the TXT/CSV file.
- Now run the script. Type “.\RemoveAccount.ps1”

Accounts are deleted as shown below.

Thanks for the most valuable post and rep. It was really helpful.This mean we dont have to use any third party tool any more.
ReplyDeleteThanks again.
Rachit
I am having an issue getting this to run correctly, I have a csv but i get an error:
ReplyDeleteRemove-ADUser : Cannot validate argument on parameter 'Identity'. The argument
is null. Supply a non-null argument and try the command again.
At line:1 char:65
+ import-csv delete.csv | foreach-object { remove-aduser -identity <<<< $_.ide
ntity }
+ CategoryInfo : InvalidData: (:) [Remove-ADUser], ParameterBindi
ngValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Activ
eDirectory.Management.Commands.RemoveADUser
Hi Rob, it seems you need to specify the full path of your csv file i.e.
Deleteimport-csv c:\delete.csv | foreach-object...
I tried this but nothing actually happens, it doen't delete any accounts for me.
ReplyDeleteJust try the given tool for ad objects deletion.
ReplyDeletehttp://adsysnet.com/asn-active-directory-manager-delete-restore.aspx
download here
http://adsysnet.com/asn-active-directory-manager-download.aspx
hello,
ReplyDeletemy script execute well but the user put in the txt file does not remove it ...
No error back
But if I do the command in the console with the name of my users that it works ...