Verify file copy in powershell
Is there an easy way to verify all files were copied correctly when calling copy-item? I thought about us开发者_如何学JAVAing the checksum on all files, but I would imagine powershell (v2) would already have something, and I can't find it.
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.
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.
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.
My current favourite solution is xcopy in combination with a hashdeep audit. Can be packed together in a small BAT-file.
you can use -verbose
Copy-Item -Path $Source -Destination $Destination -Recurse -Force -verbose
精彩评论