none
Need Powerhell script to delete multiple users from Active Directory

    Question

  • Hi 

    I am looking for powershell script to delete multiple users from AD. Basically users are located in different OUs, I have list of user samaccount names. I want to put in csv file and delete them using powershell.

    in script domain name should be mentioned, so that it will be clear for me I am running the script towards the domain.

    Please don't give me links to read and learn kind of reference links. I am purely looking for powershell script. 

    Appreciate your help..

    Friday, December 16, 2016 5:25 PM

All replies

  • Similar to:

    Import-Module ActiveDirectory
    $Users = Import-Csv C:\MyScripts\Terminated.csv
    
    ForEach ($User in $Users)
    {
        # Retrieve user to make sure they exist.
        $ADUser = Get-ADUser -Identity $User.sAMAccountName
        If ($ADUser)
        {
            # Delete the user.
            Remove-ADUser -Identity $User.sAMAccountName
        }
    }
    


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Friday, December 16, 2016 6:55 PM
  • Hi, I run this and it doesn't delete the disabled user account I have in my CSV, please help.
    Tuesday, February 5, 2019 3:41 PM
  • The script assumes that the CSV file has a header line defining the field "sAMAccountName".

    Edit: If the CSV has no header line, you can use the -Header parameter of Import-Csv to define the fields. It must define all of the fields in the file. For example.

    $Fields = "sAMAccountName","Name"
    $Users = Import-Csv C:\MyScripts\Terminated.csv -Header $Fields

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)


    Tuesday, February 5, 2019 4:26 PM