7

I am copying files from One Windows machine to another using Copy-Item in Powershell script.

But I want to wait till copy completes, Powershell Copy-Item is non-blocking call means, it just triggers copy and returns to script however I want to wait till copy completes.

Is there any way to do it ?

VarunVyas
  • 1,357
  • 6
  • 15
  • 23

3 Answers3

3

Maybe "copy-item [...] | out-null" will help to wait for completion.

"Out-null actually deletes output instead of routing it to the PowerShell console, so the script will sit and wait for the content to be deleted before moving on."

The explanation comes from ITProToday.com: https://www.itprotoday.com/powershell/forcing-powershell-wait-process-complete

Fabi
  • 31
  • 3
-3

Copy-Item does block. I just copied a 4.2GB file from a share on our gigabit network to my local machine. My PowerShell prompt hung and didn't return for several minutes.

Aaron Jensen
  • 25,861
  • 15
  • 82
  • 91
  • 9
    this is not right. copy-item does NOT block if the item is small enough to fit in the cache. Also if you are copying multiple large files with multiple copy-item commands one after another, even ones much larger than the available cache, at the end of the first file, once enough data has been written to disk to fit the rest of the first file in to cache copy-item finishes even though there is still 100's of MB to go. Watching the resource monitor of what files are being written during the change from one file to the next will show you this. – BeowulfNode42 Jan 06 '16 at 04:11
  • also on 128MiB files my Windows 2008r2 server fits about about 10 files in cache immediately, then settles down to have about 4 files in cache at any one time, after copying about 30 files. – BeowulfNode42 Jan 06 '16 at 04:14
-5

It seems like it's non-blocking here for very small files. I'm using:

Start-Sleep -s 3

To wait for the files to be copied. Not an ideal solution but it's what I got so far.

sigmaxf
  • 7,998
  • 15
  • 65
  • 125