In this post, I’ll show you how to use PowerShell to get the list of users who were created within given number of times
The PowerShell Get-ADuser cmdlet is used to get one or more Active Directory users. You can just type the cmdlet in a PowerShell window with user, it will provide you an account info.
Note that you must be logged in to Windows with a domain user account that has permission to read the user you want to list. You must also have the Active Directory module for Windows PowerShell installed on the device where you want to run the Get-ADUser cmdlet.
To use the Get-AdUser cmdlet examples covered in this article, be sure you have the following:
The Get-ADUser cmdlet gets a specified user object or performs a search to get multiple user objects.
The Identity parameter specifies the Active Directory user to get. You can identify a user by its distinguished name (DN), GUID, security identifier (SID), Security Account Manager (SAM) account name, or name. You can also set the parameter to a user object variable such as $<localUserObject> or pass a user object through the pipeline to the Identity parameter.
Get-ADUser -Filter * -SearchBase "OU=user,DC=example,DC=com"
To search for and retrieve more than one user, use the Filter or LDAPFilter parameters. The Filter parameter uses the PowerShell Expression Language to write query strings for Active Directory. PowerShell Expression Language syntax provides rich type-conversion support for value types received by the Filter parameter.
Get-ADuser -Filter {whenCreated -ge $XdaysBack}
This cmdlet retrieves a default set of user object properties. To retrieve additional properties use the Properties parameter.
The Get-Date cmdlet gets a DateTime object that represents the current date or a date that you specify. It can format the date and time in several Windows and UNIX formats. You can use Get-Date to generate a date or time character string, and then send the string to other cmdlets or programs
The Add<Interval> methods can be used to add and subtract time, for example:
Get-Date # Get Current Date (Get-Date).AddDays(1) # One day from now (Get-Date).AddDays(-1) # One day before now
So now, We use Get-ADUser command to fetch user using query "whenCreated" is greater than or equals to given interval of time.
Current date is assigned to variable $now and Interval will be assigned to variable $days, $XdaysBack will be operated date
Get-AdUser will fetch set of user with employeeid, whencreated and default Properties. List will be exported to csv file(Which was assigned to variable $outFilePath)
#Get current date $now=Get-Date #mention number of days to be added or subtracted from $now #add plus or minus sign to go back or forward in time $days = -30 $XdaysBack = ($now).AddDays($days) $outFilePath = 'C:\User-List.csv' #Loop through all filtered IDs, select distinguishedname,samaccountname,employeeid,whencreated attributes #and export it to csv file Get-ADuser -Filter {whenCreated -ge $XdaysBack} -Properties employeeid,whencreated | select distinguishedname,samaccountname,employeeid,whencreated | Export-CSV $outFilePath -Append -NoTypeInformation
There may be a situation due to audit requirements where we need to get the users list for given interval, this simple script will help to fetch you list of users in sort period of time