How to Find SharePoint Folders by Keyword Using PnP PowerShell
Question details
The user requires a PowerShell-based solution to search across SharePoint Online sites and identify document library folders that contain a specific keyword in their name.

- Product
- SharePoint Online / PnP PowerShell
- Device & OS
- not provided
- Scenario
- Locating specific document folders across multiple SharePoint sites based on a target keyword (e.g., EMP) and compiling the results.
- Observed behavior
- The user needs to retrieve a list of folders, including details like name, creation date, item count, and relative URL, and export or filter them to isolate folders containing the keyword.
Ensure you have SharePoint Administrator privileges and that the latest version of the PnP.PowerShell module is installed on your system before executing tenant-wide scripts.
Use PnP PowerShell to Retrieve and Filter Folders
Deploy a PnP PowerShell script to iterate through sites and document libraries, extracting folder properties for keyword filtering.
This approach utilizes the Get-PnPFolder cmdlet to programmatically traverse document libraries in SharePoint Online. Extracting key properties such as Name, TimeCreated, ItemCount, and ServerRelativeUrl allows you to effectively track down the required folders.
You can either filter the data directly within the PowerShell console or export the entire dataset to a CSV file for manual filtering in a spreadsheet application.
Open your PowerShell console as an Administrator. Run 'Install-Module -Name PnP.PowerShell' if you haven't already. Then, connect to your SharePoint environment using 'Connect-PnPOnline -Url https://yourtenant.sharepoint.com/sites/yoursite -Interactive'.
Set up a loop for your target document libraries. Inside the loop, run '$Folders = Get-PnPFolder -List $ListName' to retrieve all folder objects within the specified list.
Format the retrieved data by piping it to Select-Object. Use the command '$Folders | Select-Object Name, TimeCreated, ItemCount, ServerRelativeUrl' to isolate the necessary attributes.
To find a specific keyword like EMP, append a Where-Object filter: 'Where-Object { $_.Name -match "EMP" }'. To export the results for sharing, pipe the final output to 'Export-Csv -Path C:\Folders.csv -NoTypeInformation'.

View and Filter Exported SharePoint Data with WPS Office
After exporting your SharePoint folder data to a CSV using PowerShell, you need a robust spreadsheet application to filter and analyze the results. WPS Office provides a free, lightweight, and highly compatible alternative to Microsoft Office, perfect for managing your exported reports.

Frequently Asked Questions
Can I search for multiple keywords in the folder names simultaneously?
Yes, you can modify the Where-Object filter in your PowerShell script to include multiple conditions using regular expressions. For example, using '$_.Name -match "EMP|HR|FIN"' will return folders containing any of those three keywords.
Do I need site collection admin rights to run this script across all sites?
Yes, to loop through multiple sites and document libraries across a SharePoint tenant, the account executing the script must possess SharePoint Administrator permissions or be listed as a Site Collection Administrator for each target site.
Why is Get-PnPFolder not returning all items in my large library?
For extremely large document libraries, you may encounter the SharePoint list view threshold limit (typically 5,000 items). To bypass this, you may need to adjust your PowerShell script to process items in batches or utilize CAML queries for targeted retrieval.




