Forum Discussion
ellan1537
Nov 27, 2023Iron Contributor
Replace existing retention labels on emails
There are tons of emails in multiple users outlook (M365) on which Retention Labels were applied. We've came up with a new set of retention labels now. Is there a script that can remap all the ex...
- Feb 01, 2024After going over this with my team and Microsoft, seems like there is no option to do this exactly that way. Although, you could use the Purview’s Content Search to get a report on Retention labels applied in Exchange.
These are the M365 Retention Labels, not MRM tags.
VasilMichev
Nov 28, 2023MVP
Unfortunately, you cannot use the auto-apply functionality for retention labels, as it only works against messages in transit. However, you should be able to programmatically apply the new label via EWS. Here's a sample script: https://learn.microsoft.com/en-us/archive/blogs/akashb/stamping-retention-policy-tag-using-ews-managed-api-1-1-from-powershellexchange-2010
ellan1537
Nov 29, 2023Iron Contributor
I'm looking for the script for Exchange Online version. Can you please review and let me know if the below script is appropriate (just for 1 mailbox)?
# Connect to Exchange Online
Connect-ExchangeOnline
Connect-IPPSSession -UserPrincipalName "yourUPN"
# Specify the user mailbox and the old/new retention labels
$userMailbox = "email address removed for privacy reasons"
$oldRetentionLabel = "OldRetentionLabel"
$newRetentionLabel = "NewRetentionLabel"
# Get all items in the user's mailbox with the old retention label
$items = Search-Mailbox -Identity $userMailbox -SearchQuery "RetentionLabel:$oldRetentionLabel" -EstimateResultOnly $false
if ($items.Count -gt 0)
{
# Apply the new retention label to each item
foreach ($item in $items)
{
Set-RetentionCompliancePolicy -Identity $item.Identity -RetentionCompliancePolicy $newRetentionLabel
Write-Host "Updated retention label for $($item.Subject) in mailbox $userMailbox."
}
}
# Disconnect from Exchange Online
Disconnect-ExchangeOnline -Confirm:$false
# Connect to Exchange Online
Connect-ExchangeOnline
Connect-IPPSSession -UserPrincipalName "yourUPN"
# Specify the user mailbox and the old/new retention labels
$userMailbox = "email address removed for privacy reasons"
$oldRetentionLabel = "OldRetentionLabel"
$newRetentionLabel = "NewRetentionLabel"
# Get all items in the user's mailbox with the old retention label
$items = Search-Mailbox -Identity $userMailbox -SearchQuery "RetentionLabel:$oldRetentionLabel" -EstimateResultOnly $false
if ($items.Count -gt 0)
{
# Apply the new retention label to each item
foreach ($item in $items)
{
Set-RetentionCompliancePolicy -Identity $item.Identity -RetentionCompliancePolicy $newRetentionLabel
Write-Host "Updated retention label for $($item.Subject) in mailbox $userMailbox."
}
}
# Disconnect from Exchange Online
Disconnect-ExchangeOnline -Confirm:$false
- VasilMichevNov 29, 2023MVPExchange/SCC cmdlets do not work on the item level, you cannot perform such operations with them. This is why I suggested to use EWS instead.
Moreover, you cannot search by RetentionLabel... was this generated by ChatGPT?- ellan1537Nov 29, 2023Iron ContributorThe EWS script referenced above appears to be for Exchange on-premises environment. Is there similar EWS script for Exchange Online environment?
- VasilMichevNov 30, 2023MVPThe only difference between on-premises and Exchange Online is how you connect to the mailbox. The actual EWS operations remain the same.
If you need examples on how to connect to Exchange Online mailboxes via EWS, check here: https://learn.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-authenticate-an-ews-application-by-using-oauth