Skip to content

Instantly share code, notes, and snippets.

@AlexanderHolmeset
Last active June 14, 2024 08:57
Show Gist options
  • Save AlexanderHolmeset/4630a7c94bda3a2bda5eb2df024186c4 to your computer and use it in GitHub Desktop.
Save AlexanderHolmeset/4630a7c94bda3a2bda5eb2df024186c4 to your computer and use it in GitHub Desktop.
# Name: SetCalendarPermissions.ps1
# Description: This script sets the default calendar permissions for all users in a Microsoft 365 organization to LimitedRead.
# Author: Alexander Holmeset
# CoContributor: Simon Skotheimsvik, http://skotheimsvik.no, https://x.com/SSkotheimsvik
# Version: 1.0
# Date: 2021-09-01
# Blog: https://alexholmeset.blog
# X(Twitter): https://x.com/AlexHolmeset
# Set the permission level to be set on the calendars.
# Options can be found here:
# https://learn.microsoft.com/en-us/graph/api/resources/calendarpermission?view=graph-rest-1.0#calendarroletype-values
$Permission = "LimitedRead"
# Connects to Microsoft Graph with the specified scopes
Connect-MgGraph -Identity
# Generates a list of all users in the Microsoft 365 organization
$users = Get-MgUser -All -Property "id", "AssignedLicenses", "DisplayName", "Mail", "UserPrincipalName" | Where-Object { $_.AssignedLicenses.Count -gt 0 -and $_.Mail -ne $null}
# Sets default access to LimitedRead for all calendars in each user's mailbox
foreach ($user in $users) {
# Prints the user currently in focus
write-up
Write-Output "User in focus = $($user.userprincipalname)"
# Initializes the variables to store calendar permissions
$CalenderPermissions = @()
$CalenderPermissions = Get-MgUserCalendarPermission -UserId $user.id
# If the user has any calendar permissions, update them
If($CalenderPermissions){
$CalenderPermissionsMyOrg = @()
$CalenderPermissionsMyOrg = $CalenderPermissions | Where-Object {$_.EmailAddress.Name -eq "My Organization"}
# Updates the calendar permissions for the user
If($CalenderPermissionsMyOrg.Role -ne $Permission){
Update-MgUserCalendarPermission -UserId $user.id -Role $Permission -CalendarPermissionId $CalenderPermissionsMyOrg.id
# Prints the user whose calendar permissions are being set
Write-Output "- Setting permission on calendar for $($user.userprincipalname)"
}
Else{
Write-Output "- Permission already set on calendar for $($user.userprincipalname)"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment