Monday, April 9, 2012

Delete AD multiple user accounts using PowerShell

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 }



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.



6 comments:

  1. 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.

    Thanks again.
    Rachit

    ReplyDelete
  2. I am having an issue getting this to run correctly, I have a csv but i get an error:
    Remove-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

    ReplyDelete
    Replies
    1. Hi Rob, it seems you need to specify the full path of your csv file i.e.
      import-csv c:\delete.csv | foreach-object...

      Delete
  3. I tried this but nothing actually happens, it doen't delete any accounts for me.

    ReplyDelete
  4. Just try the given tool for ad objects deletion.

    http://adsysnet.com/asn-active-directory-manager-delete-restore.aspx

    download here
    http://adsysnet.com/asn-active-directory-manager-download.aspx

    ReplyDelete
  5. hello,

    my 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 ...

    ReplyDelete

Azure Policy support for remediating tags for existing resources

Use Azure policy to remediate tags for existing resources. https://azure.microsoft.com/en-us/updates/azure-provides-at-scale-tags-managem...