Verify file copy in powershell

11,104

Solution 1

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.

Solution 2

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.

Solution 3

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.

Share:
11,104
Admin
Author by

Admin

Updated on June 06, 2022

Comments

  • Admin
    Admin almost 2 years

    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.