Get All Microsoft 365 Users Created in the Last Month

Managing user accounts in Microsoft 365 can be a challenging task, especially when it comes to tracking new user additions. In this article, we'll provide a PowerShell script that retrieves all Microsoft 365 users created within the last month. This script utilizes the Microsoft Graph PowerShell module and can be a valuable tool for administrators to monitor new user accounts.


PowerShell Script

Here is the PowerShell script to fetch Microsoft 365 users created within the last month:

# Ensure the Microsoft.Graph module is installed and imported
Install-Module -Name Microsoft.Graph -Force -AllowClobber
Import-Module Microsoft.Graph

# Connect to Microsoft Graph
Connect-MgGraph -Scopes "User.Read.All"

# Calculate the date 30 days ago from today
$DateThreshold = (Get-Date).AddDays(-30)

# Fetch all users
$AllUsers = Get-MgUser -All -Property DisplayName, UserPrincipalName, CreatedDateTime

# Filter users created within the last 30 days
$RecentUsers = $AllUsers | Where-Object { [datetime]$_.CreatedDateTime -ge $DateThreshold }

# Display the results
$RecentUsers | Select-Object DisplayName, UserPrincipalName, CreatedDateTime

Script Output


Script Explanation

Install and Import the Microsoft.Graph Module

The script begins by ensuring that the Microsoft.Graph module is installed and imported for use. This module allows us to interact with Microsoft Graph API through PowerShell.

Connect to Microsoft Graph

The Connect-MgGraph cmdlet is used to authenticate and establish a connection to the Microsoft Graph API. You will be prompted to provide your credentials.

Calculate Date Threshold

The script calculates the date 30 days ago from the current date using Get-Date and AddDays(-30). This date is used as the threshold to filter users created within the last 30 days.

Fetch All Users

The Get-MgUser cmdlet fetches all users from Microsoft 365, including their display names, user principal names, and creation dates.

Filter Users

Using Where-Object, the script filters the users based on their CreatedDateTime property to include only those created within the last 30 days.

Display Results

The script then selects and displays the relevant properties (DisplayName, UserPrincipalName, CreatedDateTime) of the filtered users.


Use Cases

  • User Auditing: Regularly monitor new user accounts to ensure compliance with organizational policies.
  • Security Checks: Identify and verify new accounts to prevent unauthorized access.
  • Reporting: Generate reports on user creation trends for management review.
  • Automation: Integrate with automated workflows to trigger alerts or actions based on new user additions.

Possible Errors & Solutions

Error: Invalid or Unsupported Query

Get-MgUser : Unsupported or invalid query filter clause specified for property 'createdDateTime' of resource 'User'.
Status: 400 (BadRequest)
ErrorCode: Request_UnsupportedQuery

Solution:

  • The script initially attempts to filter users directly based on createdDateTime. This approach is unsupported. Instead, the script fetches all users and filters them within PowerShell.

Error: Authentication Issues

Connect-MgGraph : Exception of type 'Microsoft.Graph.Auth.AuthException' was thrown.

Solution:

  • Ensure you have the necessary permissions (User.Read.All) granted in your Microsoft Graph app registration.
  • Verify that your credentials are correct and you have the appropriate access rights.

Error: Throttling

Too many requests error.

Solution:

  • If you encounter throttling issues, consider implementing retry logic in your script or running the script during off-peak hours.

Conclusion

Using the Microsoft Graph PowerShell module, administrators can efficiently retrieve and monitor new user accounts created within the last month in Microsoft 365. This script provides a straightforward solution to track user additions, ensuring compliance and security within your organization. By understanding the script's components, use cases, and potential errors, you can effectively integrate this tool into your administrative toolkit.


Related Articles:

Using Get-MgDirectoryRole in Graph PowerShell
Using Get-MgUserLicenseDetail in Graph PowerShell
Using Find-GraphMgCommand in Graph PowerShell
Connect to Microsoft 365 Using PowerShell
How to Create Bulk Users in Office 365 Using Graph PowerShell?
Create Microsoft 365 Group Using Microsoft Graph PowerShell
Block Microsoft 365 User Using Microsoft Graph PowerShell
Assign Microsoft 365 License Using Graph PowerShell
Microsoft 365 User Management Using Graph PowerShell
Checking Group Membership in Microsoft 365
Bulk Assign Microsoft 365 License
Find Inactive Users in Microsoft 365
Using Powershell Graph Search Query
Using Powershell Graph Filter Query
Using Where-Object In Graph PowerShell
Using Expand Property In Graph PowerShell
Using Select Object In Graph PowerShell
Using -Contains Operator In Graph PowerShell
Add User to Multiple Microsoft 365 Groups Using Graph PowerShell
Get Microsoft 365 User Location Using Graph PowerShell
Import Microsoft 365 Groups from CSV File Using Graph PowerShell
Microsoft 365 Group User Import Using Graph PowerShell

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