How to Find Duplicate Files in PowerShell (With Example)


You can use the following syntax in PowerShell to find duplicate files in the same directory:

gci -Recurse -File | Group Name | ? { $_.Count -gt 1 }

This particular example will return the filename and count of any duplicate files in the current directory.

You could also use the following syntax to return only the full path to each duplicate file:

gci -Recurse -File | Group Name | ? { $_.Count -gt 1 } | Select -Expand group | % { $_.fullname }

The following example shows how to use this syntax in practice.

Example: How to Find Duplicate Files in PowerShell

We can use the following syntax to recursively display all files in our current directory:

gci -Recurse -File

The following screenshot shows how to use this syntax in practice:

Note: The gci command is an alias for the Get-ChildItem cmdlet, which is used to retrieve items from a directory. By using the -Recurse and -File options, we’re able to recursively retrieve only the files from each folder and subfolder in the current directory.

Suppose that we we would like to find all duplicate files in this directory.

We can use the following syntax to do so:

gci -Recurse -File | Group Name | ? { $_.Count -gt 1 }

The following screenshot shows how to use this syntax in practice:

PowerShell find duplicate file count

We can see that the file named rosters.txt occurs 2 times in the current directory.

If we’d like, we can use the following syntax to display the full path to each of these duplicate files:

gci -Recurse -File | Group Name | ? { $_.Count -gt 1 } | Select -Expand group | % { $_.fullname }

The following screenshot shows how to use this syntax in practice:

PowerShell display duplicate files

The output displays the full path to both duplicate files that have the name rosters.txt.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to List Files in Directory by Date
PowerShell: How to Get Most Recently Modified File in Directory
PowerShell: How to List All Files in Directory to Text File
PowerShell: How to Delete All Files with Specific Extension

Leave a Reply