Adding OneDrive Locations to an Existing Retention Policy

Adding OneDrive Locations to an Existing Retention Policy

  • 1

Yesterday, while working with a customer, I was asked if there is a way to programmatically add OneDrive locations to an existing retention policy.  Say, for example, you have a blanket retention policy that covers all of your tenant at a base level (which is what I typically recommend to ensure you at least have either belt or suspenders), but then you have a second set of users that need a longer policy.


As a refresher, an object in Office 365 can have multiple retention policies applied to it.  If you haven’t seen this graphic, I heartily suggest you take a deep, long look into its blue hues:

Adding OneDrive Locations to an Existing Retention Policy

If you have multiple retention and/or deletion policies applied to an object, they follow this framework.  Retention always wins.  Let’s take a simple example:

So, imagine Jim, Nestor, and Adele all work for an organization that has the previously listed policies in place.  Jim works in Sales, while Nestor and Adele work in HR.  They’re all part of the “All Employees” group, since … well, they’re employees.  Nestor and Adele, however, are part of the HR group, so HR policies apply to them.  How will the retention policies affect them differently?

Jim, Nestor, and Adele, as part of “All Employees,” will have the 3 Year All Employees retention policy applied to them as well as the 5 Year Rolling Purge All Employees policy.  In essence, what this means is that:

  • All data will be kept for a minimum of 3 years
  • After a piece of data reaches the 3 year mark, if it had been “deleted” and stored in Recoverable Items (mailbox) or Preservation Hold Library (OneDrive), it will be removed from the system.  If the item had not been deleted by the user, it will continue to exist.
  • After a piece of data reaches the 5 year mark, it will be deleted.

But wait! Adele and Nestor are also members of the HR Employees group, so they have an extra policy that applies to them: 10 Year HR Employees.  So, since Retention Always Wins, their data will live for another 5 years past the 5 Year Rolling Purge All Employees policy.  But, after 10 years, if there is no other mechanism protecting their data (such as an eDiscovery case hold or a litigation hold), data will start being purged.

Back to the question at hand … my customer has a requirement to extend the length of retention for a certain group of OneDrive sites.  If you’ve ever used the Retention interface for managing OneDrive, you’ll notice that it is NOT intuitive (meaning you can’t select users from a list and have it insert their OneDrive sites, nor do you have a browsable or searchable list of OneDrive sites).  In order to manually include or exclude a OneDrive site in a Retention Policy, you need to know the exact URL of the user’s OneDrive for Business Site.


Bring on the ‘Shell

Fortunately, there’s a way to solve this problem. And, it doesn’t even involve a lot of work for you, since I’ve already done most of it.

Get a list of OneDrive for Business sites

We don’t have a good way to do this natively.  For that, [shameless plug coming] I built the OneDrive for Business Admin tool.  There are a lot of great features in there, but we’re going to focus on one that allows us to enumerate the OneDrive sites.

The -ListOneDriveSites parameter allows you to enumerate all of the OneDrive sites in your tenant, all the OneDrive sites for users supplied in a list, or all of the users passed in the Identity parameter.

In this case, we want to add Nestor and Adele to the OneDrive – 7 Years policy in our tenant to extend their retention past the 3 year mandatory.   So, we’re going to use the OneDrive for Business Admin tool to figure out what their OneDrive sites are so we can add them to the policy.

  1. Download the OneDrive for Business Admin tool from
  2. Generate a list of OneDrive sites:
    $cred = Get-Credential
    .\OneDriveForBusinessAdmin.ps1 -Credential $cred -Tenant ems340903 -ListOneDriveSites -Identity,

    Adding OneDrive Locations to an Existing Retention Policy
    With this, now we’ll be able to explicitly add the OneDrive sites to the retention policy.

Connect to the Security & Compliance Center Powershell Endpoint

We’re going to be managing the retention policy that exists in the Security & Compliance center, so it stands to reason that we need to connect to it.  The SCC requires a different endpoint.  I have them both in a single function inside my PowerShell profile (and I recommend you do it, too).

Function o365Logon([switch]$Skype,[switch]$Compliance,$Credential,$EOP)
  If (!$Credential) { $Credential = Get-Credential }
  If ($EOP)
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri -Credential $UserCredential -Authentication Basic -AllowRedirection
    Import-PSSession $Session
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri -Credential $Credential -Authentication Basic -AllowRedirection
    Import-PSSession $Session
  Connect-MsolService -Credential $Credential
  If ($Skype)
    $SkypeSession = New-CSOnlineSession -Credential $Credential
    Import-PSSession $SkypeSession
  If ($Compliance)
    $ComplianceSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri -Credential $Credential -Authentication Basic -AllowRedirection
    Import-PSSession $ComplianceSession -AllowClobber 

Using the above function, you can connect using the credential you previously saved.

o365logon -Credential -Compliance

Update the Retention Policy

  1. First, we need to locate the retention policy.

    Adding OneDrive Locations to an Existing Retention Policy

  2. Set the retention policy name to $policy and import the new locations to add to a variable.
    $policy = 'onedrive - 7 years'
    $new = import-csv sitestoadd.csv
    $newlocations = $new.OD4BSite

    Adding OneDrive Locations to an Existing Retention Policy

  3. Update the retention policy.
    Set-RetentionCompliancePolicy -Identity $policy -AddOneDriveLocation $newlocations
  4. Verify in the portal.  Log in to and navigate to Data Governance | Retention, select the policy, and then choose Edit policy.
  5. Select Locations Applied, and then select Choose accounts under OneDrive accounts.
    Adding OneDrive Locations to an Existing Retention Policy
  6. Verify that the new accounts have been added.
    Adding OneDrive Locations to an Existing Retention Policy

Happy adding to retention policies!

Published by Aaron Guilmette

Helping companies conquer inferior technology since 1997. I spend my time developing and implementing technology solutions so people can spend less time with technology. Specialties: Active Directory and Exchange consulting and deployment, Virtualization, Disaster Recovery, Office 365, datacenter migration/consolidation, cheese.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.