How to find Active Directory User Accounts created in the last week

This is a script to find the list of Active Directory User Accounts created in, say, the last week, or even in the last year. The script exports the output to a CSV file, saves it to your desktop, and then opens the CSV file for you. 
In the script we first need to import Active Directory Module for PowerShell (installing RSAT tools on your machine will
Thanks to Aman, here is the script:
  1. <# 
  2.     Author  : Aman Dhally 
  3.     Email    : amandhally@gmail.com 
  4.     Date    : 16August2012 
  5.     Script & Purpose    : Created AD user Within Date  — to find new AD users created With-In a week 
  6.     website : http://www.amandhally.net 
  7.     twitter : https://twitter.com/#!/AmanDhally  
  8.                /^(o.o)^\  V.1 
  9.  #> 
  10.  
  11. # Import Active Directory Module, i.e. first install the RSAT Tools
  12. # http://www.amandhally.net/2012/02/13/automate-server-administration-tools/&nbsp; 
  13.     cls 
  14.     “`n” 
  15.     Write-Host ” ==> Importing Active Direcotry Module.” -ForegroundColor ‘Green’  
  16.     Import-Module -Name ActiveDirectory  
  17.  
  18. # Here we give a date 7 days before 
  19. # for 15 days, use $week = (Get-Date).AddDays(-15) 
  20. # for a month, user $week = (Get-Date).AddDays(-30) 
  21. # for a year, you can use $week = (Get-Date).AddDays(-365) 
  22.     Write-Host ” ==> Setting Date Period defined by you.” -ForegroundColor ‘Yellow’ 
  23.     $week = (Get-Date).AddDays(7) 
  24.  
  25. # Save the CSV File on users Desktop 
  26.     Write-Host ” ==> Setting file path.” -ForegroundColor ‘Magenta’ 
  27.     $filepath = “$env:USERPROFILE\desktop” 
  28.  
  29. # run on all users and save the output to csv file and the file will be saved on your desktop 
  30.   Write-Host ” ==> Running your query on all AD users.” -ForegroundColor ‘Cyan’ 
  31.     “`n” 
  32.     Get-ADUser Filter * -Properties * | ` 
  33.     where { $_.whenCreated -ge $week } | select Name,whenCreated ` 
  34.     Export-Csv -Path “$filepath\NewAdusers.csv” 
  35.  
  36. #Now open the file 
  37.     Write-Host ” ==> Opening NewAdusers.csv file.” -ForegroundColor ‘Green’ 
  38.     Invoke-Expression “$filepath\NewAdusers.csv” 
  39.  
  40. #End of the script  
  41.  ################################################### 
  42. # quote  
  43. “`n” 
  44. “`n” 
  45. Write-Host   Quote of the script   -ForegroundColor ‘Red’ 
  46. Write-Host   Give up your selfishness, and you shall find peace,” -ForegroundColor ‘Yellow’ 
  47. write-host   like water mingling with water, you shall merge in absorption. SGGS” -ForegroundColor ‘Magenta’ 
  48. “`n” 
  49. ### 
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s