I created a command to find duplicate file on folders (with file length information) in Powershell that works fine but I have problem when a file that I analyze is open.
This is the command:
Get-ChildItem -Recurse |
select-object length, fullname, @{n="Hash";e={get-filehash -algorithm MD5 -path $_.FullName -ErrorAction SilentlyContinue | Select-object -expandproperty Hash}} |
Group -Property Hash |
where {$_.Count -gt 1} |
foreach { $_.Group | select fullname, hash, length} |
Export-Csv -Path c:\temp\filelist.csv -Delimiter "`t" -NoTypeInformation
When the file is open I receive this error:
Group : Object reference not set to an instance of an object.
At line:2 char:164
+ ... tinue | Select-object -expandproperty Hash}} | Group -Property Hash |
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Group-Object], NullReferenceException
+ FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.Commands.GroupObjectCommand
The problem is that Get-FileHash fails and the execution stop when a file is open.
I want only that the files that is open ignore and the command continue.
I tried many solutions (-ErrorAction SilentlyContinue
) without success.
Please can you help me?
Thank you.
Change your code using a loop instead of
Select-Object
and error handling to skip files whereGet-FileHash
fails to obtain the hash:Another alternative that should work would be using the
-PipelineVariable
common parameter: