Category filter
PowerShell script to find, remove/rename files with duplicate filenames
IT administrators who wish to maintain a proper file naming convention for their company devices are often challenged by the presence of files with duplicate filenames. These duplicates are often created when the original files are copied from one location to another, or when some files are downloaded from the web multiple times to different locations. When multiple files share the same name, it becomes difficult for the user to identify the most recent or relevant version, leading to potential errors, slower file searches, or data inconsistencies. To prevent this, IT admins can run PowerShell scripts to find, remove, or rename files with duplicate filenames to optimize the storage space on their devices. Here’s how you can remotely deploy these scripts to multiple Windows 10/11 devices using the Execute Custom Script action of Hexnode.
PowerShell script
Identify files with duplicate filenames in a folder
1 2 3 4 5 6 7 8 9 10 |
Get-ChildItem FolderPath –File .\* -include ('*.Fileformat1’, '*.Fileformat2') -Recurse | where {$_.Name -match 'Name of the file / Snippet from the name of the file'} | select Name, FullName, @{n='Identificator';e={"$(($_.Name -split '-')[0])"}} | Group-Object Identificator | foreach { write-host "Working with group '$($_.Name)'" write-host "Files in the group:" $_.Group.FullName Write-Host "Files which should be deleted:" $_.Group.FullName | Sort-Object | Select-Object -SkipLast 1 Write-Host " " } |
The script uses the Get-ChildItem
cmdlet to find the files with duplicate filenames. Replace FolderPath
with the path of the directory where you assume the files might be stored and replace Fileformat1
and Fileformat2
with the supposed formats of the duplicate files. Within $_.Name -match 'Name of the file / Snippet from the name of the file'
, you can specify the file name or a tiny snippet of the file name. The script searches all the files in the specified directory for the given filename and displays the original file and its duplicates. It also displays the file path of the file with duplicate filename.
To give an example, you can identify text (.txt
) files and PowerPoint (.pptx
) files with duplicate filenames containing either “Budget
” or “Audit logs
” in the drive C:\
by running the script:
Get-ChildItem C:\ -File .\* -include ('*.txt', '*pptx') -Recurse | where {$_.Name -match '^(Audit logs|Budget)'} |
select Name, FullName, @{n='Identificator';e={"$(($_.Name -split '-')[0])"}} |
Group-Object Identificator | foreach {
write-host "Working with group '$($_.Name)'"
write-host "Files in the group:"
$_.Group.FullName
Write-Host "Files which should be deleted:"
$_.Group.FullName | Sort-Object | Select-Object -SkipLast 1
Write-Host " "
}
Remove files with duplicate filenames in a folder
1 2 3 4 5 6 7 8 9 10 11 |
Get-ChildItem FolderPath –File .\* -include ('*.Fileformat1’, '*.Fileformat2') -Recurse | where {$_.Name -match 'Name of the file / Snippet from the name of the file'} | select Name, FullName, @{n='Identificator';e={"$(($_.Name -split '-')[0])"}} | Group-Object Identificator | foreach { write-host "Working with group '$($_.Name)'" write-host "Files in the group:" $_.Group.FullName Write-Host "Files deleted:" $_.Group.FullName | Sort-Object | Select-Object -SkipLast 1 $_.Group.FullName | Sort-Object | Select-Object -SkipLast 1 | Remove-Item Write-Host " " } |
Similar to the previous one, the script identifies the files with duplicate filenames in a folder using folder path, file formats and file name but uses the Remove-Item cmdlet to remove the duplicate files from the system.
E.g., remove text (.txt
) files and PowerPoint (.pptx
) files with duplicate filenames containing either “Budget
” or “Audit logs
” in the drive C:\
by running the script:
Get-ChildItem C:\ -File .\* -include ('*.txt', '*pptx') -Recurse | where {$_.Name -match '^(Audit logs|Budget)'} |
select Name, FullName, @{n='Identificator';e={"$(($_.Name -split '-')[0])"}} |
Group-Object Identificator | foreach {
write-host "Working with group '$($_.Name)'"
write-host "Files in the group:"
$_.Group.FullName
Write-Host "Files deleted:"
$_.Group.FullName | Sort-Object | Select-Object -SkipLast 1
$_.Group.FullName | Sort-Object | Select-Object -SkipLast 1 | Remove-Item
Write-Host " "
}
Rename files with duplicate filenames in a folder
1 2 3 4 5 6 7 8 9 10 11 |
Get-ChildItem FolderPath –File .\* -include ('*.Fileformat1’, '*.Fileformat2') -Recurse | where {$_.Name -match 'Name of the file / Snippet from the name of the file'} | select Name, FullName, @{n='Identificator';e={"$(($_.Name -split '-')[0])"}} | Group-Object Identificator | foreach { write-host "Working with group '$($_.Name)'" write-host "Files in the group:" $_.Group.FullName Write-Host "Files to be renamed:" $_.Group.FullName | Sort-Object | Select-Object -SkipLast 1 $_.Group | Sort-Object Fullname | Select-Object -SkipLast 1 | foreach {Rename-Item -Path $_.fullname -NewName($_.Name -replace 'current name/name snippet','new name/name snippet')} Write-Host " " } |
If the admin doesn’t want to remove the file with the duplicate filename, they can rename it by running this script. Replace FolderPath
with the path of the directory where you assume the files might be stored, Fileformat1
and Fileformat2
with the supposed formats of the files, and filename
with the full name of the file or a snippet of the name. Within {Rename-Item -Path $_.fullname -NewName($_.Name -replace 'current name/name snippet','new name/name snippet')}
, replace “current name/name snippet
” and “new name/name snippet
” with the part of the file name which is to be replaced and the replacement respectively.
For example, rename text (.txt
) files and PowerPoint (.pptx
) files with duplicate filenames containing “Budget
” stored in the folder C:\
by running the script:
Get-ChildItem C:\ -File .\* -include ('*.txt', '*pptx') -Recurse | where {$_.Name -match '^(Budget)'} |
select Name, FullName, @{n='Identificator';e={"$(($_.Name -split '-')[0])"}} |
Group-Object Identificator | foreach {
write-host "Working with group '$($_.Name)'"
write-host "Files in the group:"
$_.Group.FullName
Write-Host "Files to be renamed:"
$_.Group.FullName | Sort-Object | Select-Object -SkipLast 1
$_.Group | Sort-Object Fullname | Select-Object -SkipLast 1 | foreach {Rename-Item -Path $_.fullname -NewName($_.Name -replace '.txt','.duplicate.txt')}
Write-Host " "
}
The filename of the duplicate files end with duplicate
so that they can be easily distinguished. The admin is free to replace duplicate
with a name of their choosing.