Powershell Copy-Item with progressBar

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 1 year and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
Marcel22
Posts: 5
Last visit: Tue Sep 20, 2022 8:48 am

Powershell Copy-Item with progressBar

Post by Marcel22 »

Hello to all,
I have a question, maybe someone here can help me.
I mount my ISO file and copy the files into a directory.
Now I want to copy this directory "all Files and Folders" to my USB stick, USB drive is read out before.
Question:
How can I use Copy-Item to copy my files to the USB stick incl. a progressbar for the graphical progress on my form.
I am grateful for any help.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Powershell Copy-Item with progressBar

Post by jvierra »

I would recommend using "Robocopy" to batch copy as it returns a marker for each block of files copied. Copy-Item has no way to do that. It is a blocking call and returns nothing. You can also write your own file copy mechanism using file primitives but that would require knowing how to write a systems level program. Robocopy has all of the pieces to trigger a ProgressBar.

Robocopy Documentation
leaundrejackson
Posts: 1
Last visit: Mon Aug 22, 2022 11:29 am

Re: Powershell Copy-Item with progressBar

Post by leaundrejackson »

I didn't author this function but I do have this here:

Code: [Select all] [Expand/Collapse] [Download] (Copy-File.ps1)
  1.  
  2.     param( [string]$from, [string]$to)
  3.     $ffile = [io.file]::OpenRead($from)
  4.     $tofile = [io.file]::OpenWrite($to)
  5.     Write-Progress -Activity "Copying file" -status "$from -> $to" -PercentComplete 0
  6.     try {
  7.         [byte[]]$buff = new-object byte[] 4096
  8.         [long]$total = [int]$count = 0
  9.         do {
  10.             $count = $ffile.Read($buff, 0, $buff.Length)
  11.             $tofile.Write($buff, 0, $count)
  12.             $total += $count
  13.             if ($total % 1mb -eq 0) {
  14.                 Write-Progress -Activity "Copying file" -status "$from -> $to" `
  15.                    -PercentComplete ([long]($total * 100 / $ffile.Length))
  16.             }
  17.         } while ($count -gt 0)
  18.     }
  19.     finally {
  20.         $ffile.Dispose()
  21.         $tofile.Dispose()
  22.         Write-Progress -Activity "Copying file" -Status "Ready" -Completed
  23.     }
  24.  
Hope it helps
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Powershell Copy-Item with progressBar

Post by jvierra »

And what is the problem with that? It appears to do what you want.
Marcel22
Posts: 5
Last visit: Tue Sep 20, 2022 8:48 am

Re: Powershell Copy-Item with progressBar

Post by Marcel22 »

hello
i make with robocopy

Code: Select all

process {
#count the source files
$outputBox1.text = " Robocopy wird vorbereitet. Bitte warten..."
if ($InputSource.Text -notlike $null) {
$sourcefiles=robocopy.exe $Letter $Volume /L /S /NJH /BYTES /FP /NC /NDL /TS /XJ /R:0 /W:0
If ($sourcefiles[-5] -match '^\s{3}Files\s:\s+(?<Count>\d+).*') {$filecount=$matches.Count}
$i = 1
}
$outputBox1.Focus()
$run = robocopy.exe $Letter $Volume $switchNP $switchMIR $switchLogfile | foreach {
$ErrorActionPreference = "silentlycontinue"
#calculate percentage
$i++
[int]$pct = ($i/$filecount)*100
#update the progress bar
$progressbar.Value = ($pct)
$outputBox1.AppendText($_ + "`r`n")
[void] [System.Windows.Forms.Application]::DoEvents()
}
}

end {$progressbar.Value = 100}
} #end robocopy function
my progressbar on my fom doesn't work, it only shows the 100% at the end of the copy, but not the progress. Can someone help me with this ?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Powershell Copy-Item with progressBar

Post by jvierra »

To use RoboCopy with a ProgressBar you need to use a job and the JobTracker custom control set. If you search for examples, you will find many.

Here is one example: viewtopic.php?t=11452
Marcel22
Posts: 5
Last visit: Tue Sep 20, 2022 8:48 am

Re: Powershell Copy-Item with progressBar

Post by Marcel22 »

thank you for something like this I had been looking for, everything worked I thank you.
This topic is 1 year and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked