Delete expired accounts in specific OUs

Trying to make a PS script that finds and deletes expired accounts in specific OUs

I've created this script, and it gets the users that is expired in the 4 OUs, so far so good, but I cant get my head around how to make it delete the users.


Foreach($OU in $OUs){
Search-ADAccount -AccountExpired -Searchbase $OU | Select-Object Name

Anybody that got a solution for this? :)

1 answer

  • answered 2019-02-11 20:10 Gert Jan Kraaijeveld

    This works on my machine

    Foreach($OU in $OUs){
        Search-ADAccount -AccountExpired -Searchbase $OU | Remove-ADObject -Confirm:$false