ribe.ps1

#$mailboxes = get-mailbox | where PrimarySmtpAddress -like '*@ribedyreklinik.dk'

foreach ($mailbox in $mailboxes) {
    $email = $mailbox.PrimarySmtpAddress
    "Processing $email"
    $calendarFolder = (Get-EXOMailboxFolderStatistics -Identity $email -Folderscope Calendar | Where-Object FolderType -eq 'Calendar').Name


    $permission = Get-MailboxFolderPermission -Identity $email":\"$calendarFolder | Where-Object SharingPermissionFlags -eq 'Delegate'
    $permission

    if ($permission -ne $null) {
        "removing and adding"
    #remove the permission
        Remove-MailboxFolderPermission -Identity $email":\"$calendarFolder -User $permission.User -Confirm:$false -Force

    #add new permission
        add-MailboxFolderPermission -Identity $email":\"$calendarFolder -User Alle.MESG@ribedyreklinik.dk -AccessRights Editor -Confirm:$false
        continue
    }
    else {
        "Doing nothing"
    }

}