Powershell - Get a list of AzureAD users created in the last 2 months

Joel Chambers 0 Reputation points
2023-01-13T20:57:38.8833333+00:00

Trying to get a list of users exported to CSV for users created in the last 2 months.

It's coming up completely blank, even though there are users going back to 2021 if I omit the "| where {$_.ext1 -ge $tempdate} |" section, but there are definitely users created in the last 60 days.... so I'm missing something.

$tempdate=(Get-Date).AddDays(-60)

Write-Host "Looking for users created after" $tempdate

$aadUsers = Get-AzureADUser | Select DisplayName, ObjectID
$aadUsersExt = @()

ForEach ($aadUser in $aadUsers) {
    $user = Get-AzureADUser -ObjectId $aadUser.ObjectId | Select ObjectId, DisplayName, UserPrincipalName
    $userDetail = Get-AzureADUser -ObjectId $aadUser.ObjectId | Select -ExpandProperty ExtensionProperty
    $ext1 = $userDetail.createdDateTime

    $obj = [pscustomobject]@{"DisplayName"=$user.DisplayName; "UserPrincipalName"=$user.UserPrincipalName; "Created Date"=$ext1}
    $aadUsersExt += $obj
    }


$aadUsersExt | Where {$_
Microsoft Security | Microsoft Entra | Microsoft Entra ID
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Pierre Audonnet - MSFT 10,191 Reputation points Microsoft Employee
    2023-01-13T22:35:31.1+00:00

    The AzureAD module will be soon deprecated. You can use the MgGraph module instead. Reference: https://learn.microsoft.com/en-us/powershell/microsoftgraph/migration-steps?view=graph-powershell-1.0

    Here is an example using the MgGraph module:

    $date = Get-Date (Get-Date).AddDays(-60) -Format O
    Connect-MgGraph -Scopes 'User.Read.All'
    Get-MgUser -Filter "createdDateTime ge $date" -Property Id,DisplayName,UserPrincipalName,CreatedDateTime | `
        Select-Object Id,DisplayName,UserPrincipalName,CreatedDateTime | `
        ConvertTo-Csv -NoTypeInformation | `
        Out-File -FilePath C:\temp\myusers.csv
    
    3 people found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.