Verify file copy in powershell
Asked Answered
V

6

6

Is there an easy way to verify all files were copied correctly when calling copy-item? I thought about using the checksum on all files, but I would imagine powershell (v2) would already have something, and I can't find it.

Verada answered 17/1, 2010 at 17:23 Comment(0)
H
2

No there isn't and here is why. Copy-Item is a generic cmdlet that works for all namespace providers. So the items being copied could be files, or registry settings, or IIS configuration sections, etc. Verifying a file copy is quite a bit different than verifying a copy of registry settings.

UPDATE: As pointed out by @Dave_S the XCOPY command's verification switch isn't the kind of verification you are looking for.

If you are copying text files you could use the PowerShell Compare-Object commandlet.

If you are copying binary files you could use the system fc.exe command with /b switch.

Heckman answered 18/1, 2010 at 18:20 Comment(1)
This answer is now outdated. PowerShell has had the Get-FileHash cmdlet for some time now and this is an effective and efficient method of using common methods (encryption hash) to confirm files were copied properly. This is the correct answer https://mcmap.net/q/1699999/-verify-file-copy-in-powershellBolger
T
6

I know it's an old thread, but I thought I'd post this just in case anyone else is reading it... the /v switch DOES NOT verify the data! It just makes sure that the copy is readable. See http://support.microsoft.com/kb/126457 for details.

Trainee answered 30/7, 2010 at 14:23 Comment(1)
This is a dead link now. Can you update the answer with detail as to what the /v switch applies too?Seaquake
H
2

No there isn't and here is why. Copy-Item is a generic cmdlet that works for all namespace providers. So the items being copied could be files, or registry settings, or IIS configuration sections, etc. Verifying a file copy is quite a bit different than verifying a copy of registry settings.

UPDATE: As pointed out by @Dave_S the XCOPY command's verification switch isn't the kind of verification you are looking for.

If you are copying text files you could use the PowerShell Compare-Object commandlet.

If you are copying binary files you could use the system fc.exe command with /b switch.

Heckman answered 18/1, 2010 at 18:20 Comment(1)
This answer is now outdated. PowerShell has had the Get-FileHash cmdlet for some time now and this is an effective and efficient method of using common methods (encryption hash) to confirm files were copied properly. This is the correct answer https://mcmap.net/q/1699999/-verify-file-copy-in-powershellBolger
F
2

Just an update, Powershell v4 includes Get-FileHash which can be used to verify a file was copied successfully. If the hash is the same, the file has been copied successfully. Link to TechNet Library.

I use this answer to come up with the following.

(Get-ChildItem -file -path c:\files -Recurse).FullName | foreach {get-filehash $_ -Algorithm md5} Also can be piped into Export-CSV to easily visually compare the file hashes.

Fablan answered 14/4, 2015 at 18:35 Comment(3)
Downvote for misspelling "Algorithm" and adding 5 minutes figuring out what is wrong with PowerShell. :PPugging
Coming back to comment, at least I use full param names! Instead of the shorthand -AlgFablan
I found (Get-FileHash $File_Path).hash worked effectively. No need to spell Algorithm at all :)Rhine
H
0

My current favourite solution is xcopy in combination with a hashdeep audit. Can be packed together in a small BAT-file.

Havard answered 9/1, 2012 at 14:30 Comment(0)
E
0

you can use -verbose

Copy-Item -Path $Source -Destination $Destination -Recurse -Force -verbose

Exhale answered 21/6, 2021 at 12:6 Comment(0)
P
-1

This is what I do

$from = "D:\MyProjects\myFile.dll" 
$to = "D:\DestFolder"
Xcopy $from $to /v /f /y

# /v - verifies the to file is identical to the from file.
# /f - display from and to filenames
# /y - auto comfirm override 
Pastorship answered 14/2 at 17:48 Comment(1)
/v does not compare the copied file to the original - it only confirms the file is readable.Rightward

© 2022 - 2024 — McMap. All rights reserved.