Yesterday Jon Levesque asked me if I had any posts helping to compare two SharePoint document libraries with Power Automate.
Some compare background
Table of Contents
Speed is everything!
If you need to compare two document libraries with 100s or 100s of documents you could loop through the documents in one library and then compare them with another library but that isn’t going to perform at all.
So we need a quicker way to compare two libraries. The solution in flow is simple (fast flows are always simple!)
The approach to comparing SharePoint document libraries
To make this flow perform I will create a flow that first collects all the files from the two libraries. Then the second step will be to remove any information about the files other than the file names.
So in short I just want to get a list of file names. Actually I want to get two lists of filenames. One for each library
Then when I have the two lists I need to very quickly compare the two lists.
How many people would now use an apply to each? Well you shouldn’t! unless you like to make your self a cup of coffee and while your at it make a coffee for all your colleagues.
An Apply to each is not the right option!
The implementation
The approach that I went for is quite simple. All I need in my shopping basket is:
- 1 trigger
- 2 Send an HTTP request to SharePoint actions
- 2 Select actions
- 1 Filter action

Ok, this is great! 0 seconds and I’ve got my output. So how was this done?
I’m using a manual trigger to start my flow, but you could also schedule this flow to run every hour or whatever you need it to be.
Get all files in a Library
To get all the files in my libraries I’m configuring my Send an HTTP request to SharePoint action using the SharePoint REST API as shown below. My two document libraries are called Library A and Library B.

The site address is the site where my libraries live. The Method is set to GET. and the uri is _api/web/lists/getbytitle(‘Library A’)/files
For Library B I’m repeating the same step. Just update the name of the library in the URI and you’re done for both Document Libraries!
Ok, that is the first part of 0 seconds spent already. We better hurry up!
Now the select actions for both document libraries, but before I look at the selects I want to have a look at the junk that the HTTP requests give me. Remember I just want the file names of each of the files.

I will simplify this junk a bit so that it is easier to understand the next steps.
{
"d": {
"results": [
{
...,
"Name": "Filename.xyz",
...
}
,
{
...,
"Name": "Filename2.xyz",
...
}
]
}
}
We now want to end up with an array that looks like this:
[
"Filename.xyz",
"Filename2.xyz"
]
Using the select to transform json
For both Select actions we will use the following expression in the from setting:
outputs('Send_an_HTTP_request_to_SharePoint_-_Library_A')?['body/d/results']
And for the mapping we use the item function. The item function gives you the item that is currently being processed.
item()?['Name']

For Library B just repeat the same step just replace the library name in the from configuration.
Now we are nearly there! time for some flow magic!
Filter the right items
I’m going to use a filter action with the following configuration:
From: body(‘Select_-_Library_A’)
Filter options:
- join(body(‘Select_-_Library_B’), ‘;’)
- contains
- item()

And now my filter will select give me all the file names of the documents in Library A and exist in Library B and all within less than 1 second. Did anybody say that flows do not perform so well?
Other Performance posts
https://sharepains.com/2020/02/10/1-flow-performance-tip-to-get-your-flows-out-of-the-stone-age/
https://sharepains.com/2019/06/25/performance-in-powerapps/
https://sharepains.com/2018/10/15/microsoft-flow-improve-your-flows-performance-in-a-few-easy-steps/
Hi, This was really useful and turned out to be super quick. How would you construct the Array Filter when there a multiple columns in the 2 Select outputs.? Once I have isolated the results of the Filter Array I then want to process each item and I need the other columns which have the additional data. Thanks
You could join text columns together and separate them by something like ‘###’ then afterwards you can split the data again.
This is super useful. This helps me a lot.
I’ve compared and now have 498 duplicate serial numbers from two tables containing 20 000 records in Dataverse. All in about 5 seconds.
I am now struggling to pass these back for a Delete Row operation without using a Apply to Each operation that takes several hours. Any thoughts would be very welcome!
Hi Andreas,
Rather than processing the 20000 items for deletion, create a list of 498 duplicates records. Then process those 498 records in apply to each (ideally with calling a child flow to handle the deletion). This apply to each can have the concurrency set to the maximum value of 50. It should only take a few minutes or so to get that all processed.
The following method might help building up that list of 498 as well:
https://sharepains.com/2020/03/11/pieters-method-for-advanced-in-flows/
This looks great , however am I right in thinking this will only work for two document libraries in the same 365 Tenancy and I can’t compare a Doc Library in a different external tenancy? E.g. in a migration scenario?
You can go across multiple tenants. Just create two connections. On to each tenant. A bit like the following post:
https://sharepains.com/2022/02/16/one-tenant-to-another-power-automate
Hello – i’m getting The expression “web/lists/getbytitle(‘’)/files” is not valid. How do I get the right “title” of a subfolder folder? our document library looks like this. ITALL > Documents > General > subfolder. TIA
The getbytitle should have the name of the list/library in it.
I tried using the “Documents” library name which is “Shared%20Documents” according to the URL but still getting the “is not valid”
It would need to be the Title not the url. So Documents in your example.
This is great article Thank you for it but I want to find identify duplicate documents in SharePoint when a new file got uploaded I need the flow to go through all the documents and compare the content of each file to find if it is identical content with theone which was already uploaded can you help me please
Thank you
Hi Lamiaa,
The big question to answer first is, what are identical documents. Is this two documents with the same content. Is this two files with different names but the same content? Two files with the same content but with different permissions?
Do these files live within the same library/site/folder?
Hi Pieter
Thank you so much for the prompt reply
The scope is get two files with different names but the same content – exact “content” even with different metadata like differnt name , different size etc … and they are in the same Document library but can be in different folders
Hi Pieter,
Could I tweak this for two specific folders instead of two libraries?
Hi Richie,
Yes, that would be fairly similar. You can use the Rest API to get the files in specific folders:
https://learn.microsoft.com/en-us/sharepoint/dev/sp-add-ins/working-with-folders-and-files-with-rest#working-with-folders-by-using-rest