locked
Deleting AD accounts with leaf objects RRS feed

  • Question

  • I am trying to delete AD accounts from a csv file.  The account will not delete as they have child objects. So... Trying the following:

    # Import the AD Module
    Import-Module ActiveDirectory

    $ListArray = @()

    $users = Import-Csv C:\Users.csv

    $users | foreach {

    $ListArray = $ListArray += Get-ADUser -identity $_.username

    }

    foreach($UserObject in $ListArray)

    {

    $objs = Get-ADObject -Identity $UserObject.distinguishedName

    if($objs)
       {
           #Write-Host "Removing leaf objects..."       
           $objs | Remove-ADObject -Recursive Confirm:$False
        }

    }

    What-If says this works fine.  However, when I run it without, I get prompted to OK the deletion.  I believe the -confirm works, but not recursively.  Any ideas?

    Thanks.

    Wednesday, March 30, 2016 4:43 PM

Answers

  • The -Confirm parameter should work. It is that you didn't include the dash, such as -Confirm:$false?
    • Marked as answer by bman Wednesday, March 30, 2016 6:21 PM
    Wednesday, March 30, 2016 4:55 PM