I’ve created a script for setting calendar permissions on a list of users according to the CSV file format. You can find additional details by referencing the script’s help information and the included screenshots.
The script will first attempt to connect to Office365. Please supply the correct username and password with the necessary permissions to run the script. As a best practice, start by including 2-3 users in the UserList.csv file before applying the script to all users to ensure successful execution.
UserList.csv File Format (Please make sure to keep the same headers (Mailbox,User, AccessRights) as in the Below file:
Example I
Dot Source the Script
PS> . D:\Scripts\Set-CalendarPermission.ps1
PS> Get-Help Set-CalendarPermissions
Set-CalendarPermission.ps1
Function Set-CalendarPermission {
<#
.SYNOPSIS
Apply/SET the Calendar Permissions on a list of mailboxes using permissions mentioned in a CSV file.
.DESCRIPTION
This function sets Calendar Permissions for multiple mailboxes based on a CSV file.
.NOTES
Name : Set-CalendarPermission
Author : Jatin Makhija
Version : 1.0.0
DateCreated: 30-Nov-2019
Blog : https://techpress.net
.LINK
https://techpress.net
.EXAMPLE
PS> Set-CalendarPermission -CSVFilePath <CSV File Path>
For example:
Set-CalendarPermission -CSVFilePath "C:\Temp\UserList.csv"
.EXAMPLE
Set-CalendarPermission
#>
[cmdletBinding()]
param(
[parameter(Mandatory = $true, Position = 0, HelpMessage = "Please enter the path to the CSV file.")]
[string]$CSVFilePath
)
Write-Host "Connecting to Office365" -ForegroundColor Blue -BackgroundColor White
Write-Host "Please provide your Office365 Global Administrator UserName and Password or a user with appropriate rights on EXO to update users' Calendar Permissions" -ForegroundColor Blue -BackgroundColor White
Try {
$ExCred = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $ExCred -Authentication Basic -AllowRedirection
Import-PSSession $Session
Write-Host "Connection to Office365 established" -ForegroundColor Blue -BackgroundColor White
}
Catch {
Write-Warning "User not authenticated / User credentials not accepted / User credentials not provided"
Write-Warning $_
break
}
$csvData = Import-Csv $CSVFilePath -Header Mailbox, User, AccessRights
foreach ($row in $csvData) {
$MailboxName = $row.Mailbox
$UserName = $row.User
$AccessRights = $row.AccessRights
if (-not (Get-Aduser -Filter { DisplayName -eq $MailboxName }).Enabled) {
Write-Host "$MailboxName is a disabled user account in AD" -ForegroundColor DarkYellow
} else {
if (Get-Mailbox $MailboxName -ErrorAction SilentlyContinue) {
Try {
$userList = Get-MailboxFolderPermission -Identity "$($MailboxName):\Calendar" | Select -ExpandProperty User | Select -ExpandProperty DisplayName
if ($userName -in $userList) {
$userRights = Get-MailboxFolderPermission -Identity "$($MailboxName):\Calendar" -User "$userName" | Select -ExpandProperty AccessRights
if ($userRights -eq $AccessRights) {
Write-Host "User $UserName already has $userRights on $MailboxName. No action required." -ForegroundColor Green
} else {
Write-Host "User $UserName already has permission assigned. Existing Permission: $userRights New Permission: $AccessRights" -ForegroundColor Yellow
Write-Host "Setting permissions as per the CSV file to $AccessRights"
Set-MailboxFolderPermission -Identity "$($MailboxName):\Calendar" -User "$UserName" -AccessRights $AccessRights
Write-Host "Permission $AccessRights on User $UserName set." -ForegroundColor Black -BackgroundColor White
}
} else {
Write-Host "User $Username is not assigned permission on $MailboxName. Adding permission now."
Write-Host "Processing ADD command for User $MailboxName"
if (Get-Mailbox $UserName -ErrorAction SilentlyContinue) {
Add-MailboxFolderPermission -Identity "$($MailboxName):\Calendar" -User "$UserName" -AccessRights $AccessRights
} else {
Write-Host "$UserName's mailbox does not exist. Can't add the permission." -ForegroundColor Gray
}
}
} Catch {
Write-Warning $_
}
} else {
Write-Host "$MailboxName's mailbox does not exist." -ForegroundColor Gray
}
}
}
Write-Host "Removing PSSession $Session" -ForegroundColor Blue -BackgroundColor White
Remove-PSSession $Session
}