logo
search
Others

How to Retrieve SharePoint Files by Retention Label Using PowerShell or .NET

Kushani NimanthikaKushani Nimanthika Oct 15, 2026 869 views

Question details

An organization needs to query and retrieve files across thousands of SharePoint Online sites and Teams-connected groups based on specific Microsoft Purview retention labels.

Retrieve SharePoint Files by Retention Label Using PowerShell or .NET
Product
SharePoint Online
Device & OS
not provided
Scenario
Performing a tenant-wide compliance search to programmatically locate documents tagged with a specific retention label.
Observed behavior
The user needs to construct a script or application using supported APIs or compliance search methods to achieve organization-wide document retrieval.
Before you start

Ensure your administrator account is assigned the eDiscovery Manager role in the Microsoft Purview compliance portal and that you have granted the appropriate application-level permissions (such as Sites.Read.All) in Microsoft Entra ID.

Solution 1Recommended

Use Microsoft Purview Content Search

For tenant-wide retrieval without writing custom .NET code, use the built-in Content Search feature in the Microsoft Purview compliance portal to query compliance tags.

Content Search is the most robust and native way to perform tenant-wide queries across thousands of SharePoint sites and Teams groups without dealing with API rate limits.

You can export the results as a CSV report or download the actual files depending on your compliance requirements.

1
Navigate to the Compliance Portal

Log in to the Microsoft Purview compliance portal with your admin credentials and navigate to the 'Content search' section.

2
Create a New Search

Click on 'New search' and provide a name and description for your search query.

3
Define Locations

Toggle the locations to include 'SharePoint sites' and optionally 'Exchange mailboxes' or 'Teams messages' depending on where the files are stored.

4
Build the KQL Query

In the condition builder, use the KQL (Keyword Query Language) syntax. Enter 'ComplianceTag:"YourRetentionLabelName"' in the keyword box to target the specific label.

5
Run and Export

Submit the search. Once the status shows as completed, click 'Actions' and select 'Export results' to download the file report.

Use Microsoft Purview Content Search
Free Microsoft Office alternative

Streamline Your Document Workflows with WPS Office

While complex tenant-wide SharePoint compliance searches require advanced Microsoft administration tools and custom development, WPS Office provides a lightweight, highly compatible alternative for everyday document creation, editing, and sharing.

  1. 1. Download the Installer: Visit the official WPS Office website and download the free installer for your operating system.
  2. 2. Install the Software: Run the setup file and follow the on-screen instructions to complete the fast installation process.
  3. 3. Open Your Documents: Launch WPS Office and instantly open your existing .docx, .xlsx, and .pptx files with full formatting compatibility.
Free, lightweight, and fast document processing applicationHighly compatible with Microsoft Word, Excel, and PowerPoint formatsFamiliar user interface for seamless migration without a learning curveBuilt-in PDF editing, conversion, and document protection tools
microsoft office alternative - wps office

Frequently Asked Questions

Can I use PnP PowerShell to find files by retention label?

Yes, PnP PowerShell can be used to run search queries across SharePoint sites. You can use the 'Submit-PnPSearchQuery' cmdlet and pass a KQL query like 'ComplianceTag:"YourLabelName"' to retrieve tagged files.

What is the KQL property name for a Microsoft Purview retention label?

In SharePoint and Microsoft Graph search queries, the mapped crawled property used to find Microsoft Purview retention labels is typically named 'ComplianceTag'.

Why aren't my newly labeled files showing up in the search results?

SharePoint search relies on the search crawler index. Newly applied retention labels may take some time (often between 15 minutes to 24 hours depending on server load) to be crawled and reflected in tenant-wide search results.

Where can I get help with complex Microsoft Graph API code?

For specialized development queries involving PowerShell or .NET code for Microsoft Graph API, it is recommended to post your question in the official Microsoft Q&A community under the SharePoint Development or Microsoft Graph API tags.