Powershell script to automatically remove inactive users.

This topic contains 2 replies, has 3 voices, and was last updated by Profile photo of Daniel Krebs Daniel Krebs 1 year, 4 months ago.

  • Author
  • #26566
    Profile photo of Phil Marcotte
    Phil Marcotte

    Hi Guys,

    I am wondering if something like this can be done and can't seem to find any help googling for a situation as the one I am in.

    Basically, I would need to have a script that could run in a scheduled task once per day and that takes a look at all accounts in a specific OU and if that accounts exists for more than 7 days in there, I would need to delete it. I would like to automate this process but am at a loss as to how to start.

    Thanks for your input

  • #26568
    Profile photo of Don Jones
    Don Jones

    I'd start by looking at the Get-ADUser command. It can be restricted to a specific OU (-SearchBase). You could probably apply a filter to the property containing the object's creation date and get what you want in a single query. Pipe those to Remove-ADUser, and Bob's your uncle.

  • #26570
    Profile photo of Daniel Krebs
    Daniel Krebs

    I would try the below command which is in the same module as Get-ADUser to search for inactive users and pipe to Remove-ADUser for removal.

    Search-ADAccount -AccountInactive -DateTime "5/16/2015"

You must be logged in to reply to this topic.