Functions/Get-DuplicateFileName.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
function Get-DuplicateFileName { <# .SYNOPSIS To find duplicate file names within a given folder .DESCRIPTION To find duplicate file names within a given folder. Alias for function 'Get-DupeFileName'. Will run Get-ChildItem against the given path, groups them by filename, and finds those that have a count greater than 1 .PARAMETER Path Name of a folder. Defaults to $pwd .EXAMPLE Get-DuplicateFileName -Path 'NonExistentFolder' Get-DuplicateFileName : Path [NonExistentFolder] does not exist At line:1 char:1 + Get-DuplicateFileName -Path 'NonExistentFolder' + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Get-DuplicateFileName .EXAMPLE Get-DuplicateFileName -Path 'TestFileName' Get-DuplicateFileName : Path [TestFileName] is not a folder At line:1 char:1 + Get-DuplicateFileName -Path 'TestFileName' + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Get-DuplicateFileName .EXAMPLE Get-DuplicateFileName -Path 'C:\Temp\TestFiles\' Assuming you have a single duplicate filename found in the folder 'C:\Temp\TestFiles\' Count Name Group ----- ---- ----- 2 TestFile.txt {@{Name=TestFile.txt; Directory=C:\Temp\TestFiles; LastWriteTime=9/21/2021 1:00:20 PM; LastWriteTimeUtc=9/21/2021 5:00:20 PM; Length=0; Ful... .EXAMPLE Get-DuplicateFileName -Path 'C:\Temp\TestFiles\' | Select-Object -ExpandProperty Group Name : TestFile.txt Directory : C:\Temp\TestFiles LastWriteTime : 9/21/2021 1:00:20 PM LastWriteTimeUtc : 9/21/2021 5:00:20 PM Length : 0 FullName : C:\Temp\TestFiles\TestFile.txt Name : TestFile.txt Directory : C:\Temp\TestFiles\Folder LastWriteTime : 9/21/2021 1:03:49 PM LastWriteTimeUtc : 9/21/2021 5:03:49 PM Length : 38 FullName : C:\Temp\TestFiles\Folder\TestFile.txt .NOTES Will return the following properties of each file: Name Directory LastWriteTimeUTC LastWriteTime Length FullName #> [CmdletBinding(ConfirmImpact = 'None')] [alias('Get-DupeFileName')] param( [Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)] [string] $Path = $pwd ) begin { Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]" } process { if (-not (Test-Path -Path $Path)) { Write-Error -Message "Path [$Path] does not exist" } elseif (-not (Get-Item -Path $Path).PsIsContainer) { Write-Error -Message "Path [$Path] is not a folder" } else { $File = Get-ChildItem -Path $Path -File -Recurse | Select-Object -Property Name, Directory, LastWriteTime, LastWriteTimeUTC, Length, FullName $Group = $File | Group-Object -Property Name $Group | Where-Object { $_.Count -gt 1 } } } end { Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]" } } |