Progress during large file copy (Copy-Item & Write-Progress?)
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 BitsTransferStart-BitsTransfer -Source $Source -Destination $Destination -Description "Backup" -DisplayName "Backup"
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 }}
Alternativly this option uses the native windows progress bar...
$FOF_CREATEPROGRESSDLG = "&H0&"$objShell = New-Object -ComObject "Shell.Application"$objFolder = $objShell.NameSpace($DestLocation) $objFolder.CopyHere($srcFile, $FOF_CREATEPROGRESSDLG)