You can easily add new properties and functions to object types using the extended type system. In this example, you append file objects with a new script property which creates a file hash. File hashes are an excellent way of finding duplicate files because the hash uniquely identifies the file content, and when the hash matches, you then know that these files carry the same content.
Open an editor like notepad and enter:
<Types>
<Type>
<Name>System.IO.FileInfo</Name>
<Members>
<ScriptProperty>
<Name>Hash</Name>
<GetScriptBlock>
$hashAlgorithm = new-object `
System.Security.Cryptography.MD5CryptoServiceProvider
$stream = $this.OpenRead()
$hashByteArray = $hashAlgorithm.ComputeHash($stream)
$stream.Close()
"$hashByteArray"
</GetScriptBlock>
</ScriptProperty>
</Members>
</Type>
</Types>
<Type>
<Name>System.IO.FileInfo</Name>
<Members>
<ScriptProperty>
<Name>Hash</Name>
<GetScriptBlock>
$hashAlgorithm = new-object `
System.Security.Cryptography.MD5CryptoServiceProvider
$stream = $this.OpenRead()
$hashByteArray = $hashAlgorithm.ComputeHash($stream)
$stream.Close()
"$hashByteArray"
</GetScriptBlock>
</ScriptProperty>
</Members>
</Type>
</Types>
Save this as hash.ps1xml and update PowerShell:
Update-TypeData c:\…\hash.ps1xml
Note that the hash logic will only execute when you actually use the hash property (and may take some time for large files):
Dir $home -include *.ps1 -rec |
Sort-Object Hash |
Format-Table FullName, Hash
Sort-Object Hash |
Format-Table FullName, Hash
Dir $home -include *.ps1 -rec |
Sort-Object Hash |
Format-Table FullName, Hash
Sort-Object Hash |
Format-Table FullName, Hash
You may want to use Group-Object to find duplicates:
Dir $home -include *.ps1 -rec |
Group-Object Hash |
Where-Object { $_.Count -gt 1 }
Group-Object Hash |
Where-Object { $_.Count -gt 1 }