April 18, 2024
powershell

Powershel ile duplicate dosyaları bulma

Powershell ile duplicate dosyaları bulmak son derece kolay. Duplicate yani eşdeğer dosyaları bulmak için tek yapmanız gereken Powershell ISE console açarak aşağıdaki kodu yapıştırmak. Sonrasında ise hangi path altında dublicate dosyaları bulmak istiyorsanız o yolu girmek.

Peki Powershell aynı olan dosyaları neye göre buluyor dosya ismine göre mi? yoksa dosya size’ı na göre mi. Her ikisi de değil arkadaşlar. Powershell duplicate olan dosyaları ararken hash codelarına bakıyor ve hash kodları birebir aynı ise bu dosyaların eşdeğer olduğunu anlıyor.

Eğer path olarak c:\ değerini verirseniz ki verebilirsiniz. Bazı sistem dosyalarına ulaşamayacağı için script çalışırken hata alabileceğinizi bilmelisiniz.

Powershel ile duplicate dosyaları bulma

Aşağıda çalıştırdığım scriptin çıktısını görebilirsiniz.

C:\gerial\test folderı içerisinde duplicate olan dosyalarımı arattım ve sonucu ekranda görebilirsiniz. 2 kayıt bulundu.


############# Find Duplicate Files based on Hash Value ###############
''
$filepath = Read-Host 'Enter file path for searching duplicate files (e.g. C:\Temp, C:\)'
If (Test-Path $filepath) {
''
Write-Warning 'Searching for duplicates ... Please wait ...'

$duplicates = Get-ChildItem $filepath -File -Recurse
-ErrorAction SilentlyContinue |
Get-FileHash |
Group-Object -Property Hash |
Where-Object Count -GT 1

If ($duplicates.count -lt 1)

{
Write-Warning 'No duplicates found.'
Break ''
}

else {
Write-Warning "Duplicates found."
$result = foreach ($d in $duplicates)
{
$d.Group | Select-Object -Property Path, Hash
}

$date = Get-Date -Format "MM/dd/yyy"
$itemstomove = $result |
Out-GridView -Title

"Select files (CTRL for multiple) and press OK. Selected files will be moved to C:\Duplicates_$date"
-PassThru

If ($itemstomove)

{
New-Item -ItemType Directory

-Path $env:SystemDrive\Duplicates_$date -Force
Move-Item $itemstomove.Path
-Destination $env:SystemDrive\Duplicates_$date -Force
''
Write-Warning

"Mission accomplished. Selected files moved to C:\Duplicates_$date"

Start-Process "C:\Duplicates_$date"
}

else
{
Write-Warning "Operation aborted. No files selected."
}
}
}
else
{
Write-Warning `
"Folder not found. Use full path to directory e.g. C:\photos\patrick"
}

Bir yanıt yazın