PowerShell: Get created date for Microsoft 365/Azure AD users

Recently I had a need in a project to get the dates that users were created/added to Microsoft 365, so it would be possible to get some statistics on how many users were added per period. As the number of users was not that big, the quicker solution was to figure out a way using Azure AD PowerShell.

Prerequisite

Azure AD Powershell module installed.

The simpler solution

Unfortunately, the Get-AzureADUser cmdlet doesn’t bring the created date info.

Another cmdlet can be used in combination with the one mentioned above: Get-AzureADExtension.

With the Get-AzureADExtension we can get additional properties for a single user. By combining both, it’s possible to get the values for all the users in a single script.

Syntax for getting the created date for a single user:

(Get-AzureADUserExtension -ObjectId "UserID").Get_Item("createdDateTime"

To get the created date for all users and export the data to a CSV file located in the same folder where your script is saved, you can use the following script:

Connect-AzureAD 

$PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent 

$usrs = Get-AzureADUser -All:$true 

$usrs | ForEach-Object{

    Write-Host "Getting created date for" $_.UserPrincipalName

    $_ | Add-Member -MemberType NoteProperty -Name "CreatedDateTime" `

    -Value (Get-AzureADUserExtension -ObjectId $_.ObjectId).Get_Item("createdDateTime")

} 

$usrs | Export-CSV "$PSScriptRoot\userslist.csv" 

Disconnect-AzureAD 

This script will retrieve the list of users, iterates through each of them, adds a property “CreatedDateTime” with the created date value obtained with the Get-AzureADUserExtension cmdlet and finally exports it to a CSV file.

Considerations

As the script iterates through the list of all Azure AD users and executes a request to get the extensions and created date for each, it’ll be a heavy operation. In my case, as it was a single execution for a tenant with around 5000 users only, I didn’t worry much about performance (it took a few minutes to finish executing), but be aware of potential performance/throttling issues for a tenant with a large number of users.

3 comments

Leave a Reply

Your email address will not be published. Required fields are marked *