Forum Discussion
MKMVMN
May 15, 2024Copper Contributor
Add a calendar ics with Powershell
Hello I need to add a shared calendar (ics) for around thirty people The calendar is from a company A, shared with company B. I want to add this calendar (.ics) automatically for all user...
LeonPavesic
May 16, 2024Silver Contributor
Hi MKMVMN,
you can try to download the .ics file and then add it tu multiple users with this script:
# Install the Exchange Online module if not already installed
Install-Module -Name ExchangeOnlineManagement
# Import the necessary modules
Import-Module ExchangeOnlineManagement
# connect to Exchange Online
Connect-ExchangeOnline
# Path to the .ics file
$icsFilePath = "C:\path\to\shared_calendar.ics"
# List of user email addresses
$userEmails = @(
"email address removed for privacy reasons",
"email address removed for privacy reasons",
"email address removed for privacy reasons"
)
# Loop through each user and add the .ics calendar
foreach ($userEmail in $userEmails) {
try {
# Access the user's mailbox
$mailbox = Get-Mailbox -Identity $userEmail
if ($mailbox) {
# Load the .ics file
$icsContent = Get-Content -Path $icsFilePath -Raw
# Add the .ics calendar to the user's mailbox
Add-MailboxCalendarFolder -Identity $mailbox -Name "Shared Calendar" -Calendar $icsContent
Write-Output "Successfully added calendar to $userEmail"
} else {
Write-Output "Mailbox not found for $userEmail"
}
} catch {
Write-Output "Error adding calendar to $userEmail: $_"
}
}
# Disconnect from Exchange Online
Disconnect-ExchangeOnline -Confirm:$false
- Ensure the path to the .ics file is correct.
- Make sure you have the necessary permissions to access and modify the users' calendars.