Want to wait till copy complete using Powershell Copy-Item
Asked Answered
T

3

8

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 ?

Tellus answered 31/8, 2012 at 7:57 Comment(2)
This is just plain wrong. Copy-Item does indeed block. This should probably be closed.Canara
Copy-Item is non-blocking. If i fire a copy-item command and check if path exists immediatley, it returns falseTuneless
F
-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.

Fuliginous answered 31/8, 2012 at 18:23 Comment(2)
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.Tautologism
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.Tautologism
C
4

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

Campion answered 19/1, 2019 at 21:34 Comment(0)
F
-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.

Fuliginous answered 31/8, 2012 at 18:23 Comment(2)
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.Tautologism
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.Tautologism
F
-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.

Fancier answered 22/7, 2018 at 15:22 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.