locked
Powershell copy with streams RRS feed

  • Question

  • Hello,

    I am using the following to copy items in powershell as the lack of a progress bar for Copy-Item was noticed:

    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 = [long]$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 ([int]($total/$ffile.Length* 100))
                }
            } while ($count -gt 0)
        }
        finally {
            $ffile.Dispose()
            $tofile.Dispose()
            Write-Progress -Activity "Copying file" -Status "Ready" -Completed
        }
    }

    However does anyone know of a better way of doing this that will have the ability to copy files & folders? Or any other features that are important (e.g. check if the file successfully transferred, connects if the connections drops etc...) I dont want to use programs outside of powershell itself (so no robocopy for example) 

    I am using it to copy files from a local machine to a remote share on another windows machine. 

    Monday, June 3, 2019 8:40 PM

Answers

All replies

  • Also, Start-BitsTransfer will display a progress bar.

    Monday, June 3, 2019 8:47 PM
  • I've never actually used Bits, are there any drawbacks to using it that you can think of?
    Monday, June 3, 2019 8:49 PM
  • Not that I know of.
    Monday, June 3, 2019 8:58 PM
  • RoboCopy does this faster and with all options in one line.  It is also multi-threaded.


    \_(ツ)_/

    Monday, June 3, 2019 10:01 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Friday, June 14, 2019 6:26 AM