Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Progress during large file copy (Copy-Item & Write-Progress?)

Is there any way to copy a really large file (from one server to another) in PowerShell AND display its progress?

There are solutions out there to use Write-Progress in conjunction with looping to copy many files and display progress. However I can't seem to find anything that would show progress of a single file.

Any thoughts?

like image 566
Jason Jarrett Avatar asked Mar 12 '10 16:03

Jason Jarrett


2 Answers

It seems like a much better solution to just use BitsTransfer, it seems to come OOTB on most Windows machines with PowerShell 2.0 or greater.

Import-Module BitsTransfer Start-BitsTransfer -Source $Source -Destination $Destination -Description "Backup" -DisplayName "Backup" 
like image 76
Nacht Avatar answered Oct 29 '22 15:10

Nacht


I haven't heard about progress with Copy-Item. If you don't want to use any external tool, you can experiment with streams. The size of buffer varies, you may try different values (from 2kb to 64kb).

function Copy-File {     param( [string]$from, [string]$to)     $ffile = [io.file]::OpenRead($from)     $tofile = [io.file]::OpenWrite($to)     Write-Progress -Activity "Copying file" -status "$from -> $to" -PercentComplete 0     try {         [byte[]]$buff = new-object byte[] 4096         [long]$total = [int]$count = 0         do {             $count = $ffile.Read($buff, 0, $buff.Length)             $tofile.Write($buff, 0, $count)             $total += $count             if ($total % 1mb -eq 0) {                 Write-Progress -Activity "Copying file" -status "$from -> $to" `                    -PercentComplete ([long]($total * 100 / $ffile.Length))             }         } while ($count -gt 0)     }     finally {         $ffile.Dispose()         $tofile.Dispose()         Write-Progress -Activity "Copying file" -Status "Ready" -Completed     } } 
like image 45
stej Avatar answered Oct 29 '22 14:10

stej