# Powershell script to automatically remove inactive users.

**URL:** <https://forums.powershell.org/t/powershell-script-to-automatically-remove-inactive-users/4384>\
**Category:** PowerShell Help\
**Created:** [June 17, 2015, 1:19am UTC](https://forums.powershell.org/t/powershell-script-to-automatically-remove-inactive-users/4384 "2015-06-17T01:19:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![phil-marcotte](https://avatars.discourse-cdn.com/v4/letter/p/c57346/32.png) [@phil-marcotte](https://forums.powershell.org/u/phil-marcotte)\
**Post date:** [June 17, 2015, 1:19am UTC](https://forums.powershell.org/t/powershell-script-to-automatically-remove-inactive-users/4384/1 "2015-06-17T01:19:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [June 17, 2015, 1:22am UTC](https://forums.powershell.org/t/powershell-script-to-automatically-remove-inactive-users/4384/2 "2015-06-17T01:22:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![daniel-krebs](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/daniel-krebs/32/484_2.png) [@daniel-krebs](https://forums.powershell.org/u/daniel-krebs)\
**Post date:** [June 17, 2015, 2:14am UTC](https://forums.powershell.org/t/powershell-script-to-automatically-remove-inactive-users/4384/3 "2015-06-17T02:14:24Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:45pm UTC](https://forums.powershell.org/t/powershell-script-to-automatically-remove-inactive-users/4384/4 "2024-05-16T20:45:21Z")

</div>


