Thursday, August 16, 2012

Powershell & active directory : Find all AD users account those are created between predefined period of time.


Reporting,Monitoring and the Documentation are few of the major tasks which most of the IT admin does like me. If you know how to do scripting it saves lots of time. For all IT guys those primarily works on Windows Platform for them i think learning of Powershell is must, because if you know Powershell you can automate or script most of the daily to daily tasks. 

Powershell is like  Swiss Army knife.

My todays task was to create a list of all active directory users account those are created within a week. It is easy but worth sharing.

Make sure you have RSAT tools installed before running the Active Directory based cmdlets.

Today is 16 August and i want to find a users those are create within a week so if we minus 7 days we got 9 August, so we want to know how many users created with-in the time period of 9th - 16 August.

Lets Start.

First we need to import Active Directory module.

Import-Module -Name ActiveDirectory 


Now we need to define date. The date will be sever days earlier, this is easy we just need to minus 7 days from the current date. Get-Date support a method .AddDays by which you can add or minus days.

$week = (Get-Date).AddDays(-7)

when you run the $week variable we can get a 7 days earlier date.

16-08-2012 18-58-11

Good. Now our next step is to find users. Now we are going to use Get-ADUser , and then using -Filter * to search and find all users, and we are using -Properties * so that it expand all properties of the user, and then we are piping the output to where cmdlet and then are are choosing only those users from the output who’s $_.whenCreated property is greater then or Equal to $week.

Get-ADUser -Filter * -Properties * | where { $_.whenCreated -ge $week }

We have the output now., but lets format a more little bit.

16-08-2012 19-02-03

let add more one more pipe to the command, pipe it to select Cmdlet and choose to show only, Name and When Created Property.

Get-ADUser -Filter * -Properties * | where { $_.whenCreated -ge $week } | select Name,whenCreated

Not is looks nice isn’t ?

16-08-2012 19-15-39

Powershell Script for this.

To make this task more easy , i have create a powershell script which can do this task, and the script with export all the users Name, When created to a CSV file and save that on your Desktop.

You can download the script from this link : 


Aman Dhally

join aman on facebook Join aman on Linkedin follow aman on Twitter

1 comment:

  1. Really very informative, it describes that how find active directory user accounts which have been created between predefined period of time. I tested this active directory cleaner tool ( ) which helps to know how many users created with in the predefined time and generate the complete report based on it.