đź”§ New: User Management Graph PowerShell Toolkit

Simplify user tasks like bulk creation, updates, password resets, deletions, license checks & more — all from one place.

🚀 Launch Toolkit

List Recently Created Mailboxes in Your Tenant Using Graph PowerShell

When managing a growing Microsoft 365 environment, it's essential to track newly provisioned mailboxes—especially to ensure onboarding consistency, license assignments, and security baselines. This Graph PowerShell script helps you list recently created mailboxes for your tenant.


The Script

# Connect to Microsoft Graph with necessary permissions
Connect-MgGraph -Scopes "User.Read.All", "Directory.Read.All"
                                
# Set the threshold for 'recently created' (last 30 days)
$startDate = (Get-Date).AddDays(-30).ToString("yyyy-MM-ddTHH:mm:ssZ")
                                
# Step 1: Fetch recently created tenant users (UserType = Member)
$recentUsers = Get-MgUser -All `
-Filter "UserType eq 'Member' and createdDateTime ge $startDate" `
-Property DisplayName, UserPrincipalName, Mail, CreatedDateTime
                                
# Step 2: Filter locally to keep only mail-enabled users
$recentMailboxes = $recentUsers | Where-Object { $_.Mail -ne $null -and $_.Mail -ne "" } |
Select-Object DisplayName, UserPrincipalName, 
@{Name = "Email"; Expression = { $_.Mail }},
@{Name = "Created On"; Expression = { $_.CreatedDateTime }}
                                
# Step 3: Display the results
$recentMailboxes | Format-Table -AutoSize
                            

How the Script Works

Let's break it down step-by-step:

  1. Connect-MgGraph
  2. Establishes a secure session with Microsoft Graph. You'll need delegated permissions like:

    • User.Read.All
    • Directory.Read.All

    đź’ˇ Tip: Run Disconnect-MgGraph before reconnecting if you're switching accounts.

  3. Set the Creation Threshold
  4. $startDate = (Get-Date).AddDays(-30).ToString("yyyy-MM-ddTHH:mm:ssZ")

    This defines the window (last 30 days) for evaluating newly created users.

  5. Fetch Tenant Users Only
  6. -Filter "UserType eq 'Member' and createdDateTime ge $startDate"

    This excludes all Guest users (UserType eq 'Guest'), such as external collaborators using Gmail, Yahoo, etc.

  7. Filter Users with Mailboxes
  8. Where-Object { $_.Mail -ne $null -and $_.Mail -ne "" }

    Since Graph API doesn’t support filtering on mail ne null directly, this step is handled in PowerShell after fetching the results.

  9. Present the Final Output
  10. Select-Object DisplayName, UserPrincipalName, Email, Created On

    Neatly organizes the results in a readable table format.


Further Enhancements

You can build on this script in several ways:

  • Export to CSV:
  • $recentMailboxes | Export-Csv -Path "RecentMailboxes.csv" -NoTypeInformation -Encoding UTF8
  • Send via Email using Send-MgUserMessage (with attachment)
  • Change the timeframe (e.g., last 7 days or last 90 days)
  • Include licensing info by piping into Get-MgUserLicenseDetail
  • Schedule the script in Task Scheduler or Azure Automation for routine reporting

Possible Errors & Solutions

Error Cause Solution
Filter operator 'NotEqualsMatch' is not supported You tried to use mail ne null in the -Filter Graph API doesn’t support this. Do the null-check in PowerShell instead.
Insufficient privileges to complete the operation Missing Graph permissions Ensure you have delegated User.Read.All and Directory.Read.All.
Connect-MgGraph not recognized Microsoft Graph module not installed Run Install-Module Microsoft.Graph -Scope CurrentUser

Conclusion

This Graph PowerShell script gives you a clean and efficient way to track recently created tenant mailboxes, excluding external/guest users. It’s ideal for monitoring provisioning processes, validating licensing, or auditing new account creation trends in your Microsoft 365 tenant.


Graph PowerShell Explorer Widget

20 Graph PowerShell cmdlets with easily accessible "working" examples.


Permission Required

Example:


                


                


                

© m365corner.com. All Rights Reserved. Design by HTML Codex