Get-MgUserEvent: How to Retrieve User Calendar Events with Graph PowerShell

This guide demonstrates how to use the Get-MgUserEvent cmdlet in Microsoft Graph PowerShell to retrieve calendar events for a user. Learn how to list events, filter events by ID, and export event details with practical examples

The Get-MgUserEvent cmdlet is a powerful tool in Microsoft Graph PowerShell that allows administrators to access and manage calendar events for users in Microsoft 365. In this article, we'll explore the syntax, usage examples, tips, use cases, possible errors, and solutions related to this cmdlet.


Cmdlet Syntax

Get-MgUserEvent -UserId <String> [-EventId <String>]
  • -UserId: UPN or User Id of the user whose calendar events you wish to query.
  • -EventId: Event Id of the calendar event you wish to view in detail.

Usage Examples

Querying All Events for a User

$userId = "user@example.com"
$events = Get-MgUserEvent -UserId $userId
$events | ForEach-Object {
    [PSCustomObject]@{
        Subject = $_.Subject
        Start   = $_.Start.DateTime
        End     = $_.End.DateTime
        Id      = $_.Id
    }
} | Select-Object Subject, Start, End, Id

This command lists all events in a user's default calendar. Replace "user@example.com" with the actual User UPN.

PowerShell command output displaying all user calendar events retrieved using Get-MgUserEvent in Microsoft Graph PowerShell

Querying a Specific Event

$userId = "samadmin@7xh7fj.onmicrosoft.com"
$eventId = "event-id"
$event = Get-MgUserEvent -UserId $userId -EventId $eventId
$eventDetails = [PSCustomObject]@{
    Subject    = $event.Subject
    Start      = $event.Start.DateTime
    End        = $event.End.DateTime
    Location   = $event.Location.DisplayName
    Attendees  = ($event.Attendees | ForEach-Object { $_.EmailAddress.Address }) -join " "
}
$eventDetails | Format-List

This command retrieves details of a specific event using its ID. Replace "event-id" with the actual event ID.

PowerShell script fetching details of a specific user calendar event using Get-MgUserEvent with an Event ID.

Filtering Events by Subject

You can filter calendar events based on their subject using the -Filter parameter. This is particularly useful when you're looking for events with specific keywords in their titles.

$userId = "user@example.com"
    $subjectKeyword = "Project Update"
    $filteredEvents = Get-MgUserEvent -UserId $userId -Filter "contains(subject, '$subjectKeyword')"
    $filteredEvents | ForEach-Object {
            [PSCustomObject]@{
                Subject = $_.Subject
                Start   = $_.Start.DateTime
                End     = $_.End.DateTime
                Id      = $_.Id
            }
    } | Format-Table Subject, Start, End, Id
Filtered PowerShell results showing calendar events matching a specific subject keyword using Get-MgUserEvent.

Cmdlet Tips

  • Permissions: Ensure you have the necessary permissions to access user calendar events. For delegated access, Calendars.Read or Calendars.ReadWrite permissions are required. For application access, appropriate application permissions must be granted.
  • Filtering: The -Filter parameter allows you to refine your queries. For instance, you can filter events by start time, end time, or subject keywords. This is useful for retrieving specific events without processing unnecessary data.
  • Expand Properties: Use the -ExpandProperty parameter to retrieve additional properties that are not returned by default. This is helpful when you need more detailed information about the events.

Use Cases

  • Event Management: Administrators can utilize this cmdlet to manage and audit user calendar events, ensuring compliance with organizational policies
  • Automation: Automate tasks such as sending reminders for upcoming events, generating event reports, or syncing events with other systems
  • Troubleshooting: Identify and resolve issues related to user calendar events, such as missing or duplicate entries.

Possible Errors & Solutions

Error: 404 (NotFound) - ErrorItemNotFound

Cause: The specified event was not found.

Solution: Verify the event ID is correct and exists in the user's calendar. List all events to check for the correct event ID.

Error: 403 (Forbidden) - ErrorAccessDenied

Cause: Insufficient permissions to access the calendar events.

Solution: Ensure the necessary permissions (Calendars.Read or Calendars.ReadWrite) are granted to the user or application.

Error: 400 (Bad Request) - ErrorInvalidRequest

Cause: The request is malformed or contains invalid parameters.

Solution: Check the syntax and ensure all required parameters are correctly specified.


Frequently Asked Questions

1. What is Get-MgUserEvent used for?

Get-MgUserEvent is a Microsoft Graph PowerShell cmdlet used to retrieve events from a user’s calendar, including event details like subject, attendees, and location.

2. How can I retrieve all calendar events for a user?

Use the following command to fetch all calendar events:

Get-MgUserEvent -UserId "<UserPrincipalName>" -All

3. How can I filter events by a specific date range?

Use the -Filter parameter to filter events by start and end date. Example:

Get-MgUserEvent -UserId "<UserPrincipalName>" -Filter "start/dateTime ge '2023-11-01T00:00:00Z' and end/dateTime le '2023-11-30T23:59:59Z'"

4. Can I export calendar events to a CSV file?

Yes, use this script to export event details like subject, attendees, and start time:

$Events = Get-MgUserEvent -UserId "<UserPrincipalName>" -All
$Events | Select-Object Subject, Start, End, Attendees | Export-Csv -Path "C:\Path\To\CalendarEvents.csv" -NoTypeInformation

5. What permissions are required to retrieve calendar events using Get-MgUserEvent?

You need the Calendars.Read or Calendars.ReadWrite permission in Microsoft Graph PowerShell. Ensure appropriate permissions are granted in Azure AD.


Conclusion

The Get-MgUserEvent cmdlet is an essential tool for managing and automating calendar events in Microsoft 365. By understanding its syntax, usage, and potential errors, administrators can effectively leverage this cmdlet to enhance their productivity and streamline event management processes.


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