Skip to content

Instantly share code, notes, and snippets.

@maravedi
Created October 1, 2018 20:48
Show Gist options
  • Save maravedi/fcb9d5ff1c3f99073db674e6c1c2ab37 to your computer and use it in GitHub Desktop.
Save maravedi/fcb9d5ff1c3f99073db674e6c1c2ab37 to your computer and use it in GitHub Desktop.
Get AD Accounts Created Since
<#
.SYNOPSIS
This script takes either a specific date or a number of days, weeks, or months to determine how many and, optionally, which AD accounts were created since that point in time.
.DESCRIPTION
This script has two methods of calculating a date, and it depends on which parameters are used when running it.
The first method is using a relative number of, days, weeks, or months. These can be combined as well.
The second method is using a specific date. If this method is used, then any of the relative date parameters are ignored.
.PARAMETER DaysBack
This relative date parameter takes an integer and is used to calculate the number of days to go back from the current date.
.PARAMETER WeeksBack
This relateive date parameter takes an integer and is used to calculate the number of weeks to go back from the current date. Note that there is no built-in AddWeeks method for the PowerShell Date object, so the value of this parameter is multiplied by 7 to get the number of days that would be in the specified number of weeks.
.PARAMETER MonthsBack
This relative date parameter takes an integer and is used to calculate the number of months to go back from the current date.
.PARAMETER SinceSpecificDate
This parameter takes a string and, if it is a valid date, it will use the calculated DateTime value and ignore any of the relative date parameters.
.PARAMETER Details
This parameter is an optional switch to indicate whether the AD account details should be returned. If this parameter is not specified, then only a count of the matching AD accounts will be returned.
.EXAMPLE
.\Get-NewADAccounts.ps1 -Daysback 1
1 user(s) created since 09/30/2018 00:00:00
.EXAMPLE
.\Get-NewADAccounts.ps1 -DaysBack 3 -WeeksBack 2 -MonthsBack 4
46 user(s) created since 6/1/2018 00:00:00
.EXAMPLE
.\Get-NewADAccounts.ps1 -SinceSpecificDate 01/01/2018
202 user(s) created since 1/1/2018 00:00:00
.EXAMPLE
.\Get-NewADAccounts.ps1 -WeeksBack 1 -Details
21 user(s) created since 9/24/2018 00:00:00
Name UserName Email Created
---- -------- ----- -------
John Doe jdoe John.Doe@contoso.com 10/1/2018 12:58:46
Jane Doe jdoe0 Jane.Doe@contoso.com 9/25/2018 08:02:11
.
.
.
.NOTES
Author: David Frazer - https://github.com/maravedi
Date: 10/1/2018
#>
[CmdletBinding()]
Param(
# Relative Date parameters
[Int]$DaysBack = 0,
[Int]$WeeksBack = 0,
[Int]$MonthsBack = 0,
# Specific Date paremeter
[String]$SinceSpecificDate,
# Display details of the matching accounts
[Switch]$Details
)
Try {
Import-Module ActiveDirectory -ErrorAction Stop
} Catch {
Write-Error "Could not import the ActiveDirectory module. Make sure you have RSAT installed first, then try running this again:`n`nRSAT for Windows 10 can be downloaded here: https://www.microsoft.com/en-us/download/details.aspx?id=45520"
Exit 1
}
# Get the current date
# We're ensuring that the hour, minute, second, and milliseconds are set to zero so the query is as inclusive as possible
# for the given date.
$Date = Get-Date -Hour 0 -Minute 0 -Second 0 -Millisecond 0
If(!$SinceSpecificDate) {
# Move back the specified number of days and re-assign to the Date variable
$Date = $Date.AddDays([Math]::Abs($DaysBack) * -1)
# Move back the specified number of weeks and re-assign to the Date variable
$Date = $Date.AddDays((7 * [Math]::Abs($WeeksBack)) * -1)
# Move back the specified number of months and re-assign to the Date variable
$Date = $Date.AddMonths([Math]::Abs($MonthsBack) * -1)
} Else {
Try {
# Ensure the input is atlest close to a valid date, and it it uses backslashes instead, replace them with forward slashes
If(($SinceSpecificDate -Split '/').Count -eq 3) {
$Date = [DateTime]$SinceSpecificDate
} ElseIf(($SinceSpecificDate -Split '\\').Count -eq 3) {
$Date = [DateTime](($SinceSpecificDate -Split '\\') -Join '/')
} ElseIf(($sinceSpecificDate -Split '-').Count -eq 3) {
$Date = [DateTime]$SinceSpecificDate
}
} Catch {
# A catchall that will trigger an error to be output in the next If-Else block
$Date = $False
}
}
If($Date -is "DateTime") {
# If the Date variable is a valid DateTime, then run the query on AD using the date in a filter
$Users = Get-ADUser -Properties Created -Filter "Created -gt '$Date'"
} Else {
# All of our attempts to convert the input string to a valid date failed. Let the user know and exit.
Write-Error "A valid date could not be calculated."
Exit 1
}
# Using Measure-Object instead of the built-in .Count method because it's more reliable when an object has only one item.
$Count = $Users | Measure-Object | Select -Expand Count
# Output to the console the number of users found
Write-Host "$Count user(s) created since $Date"
If($Details) {
# If the user specified the Details parameter, return the AD query results too.
Return $Users | Select Name, @{N="UserName";E={$_.SamAccountName}}, @{N="Email";E={$_.UserPrincipalName}}, Created
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment