3

I am using Copy-Item command to copy the multiple files from one location to another. In that, I want to get the count of files that are copied to destination.

If the above one is not possible, can we get 'True' if at least one file is copied and 'False' if none of the files are copied. I tried this but could not able to get exact one.

Thanks, Dinesh

Dinesh Kumar
  • 85
  • 2
  • 7

2 Answers2

4

use -passthru to create output for commands that normally run silent.

(Copy-Item -Recurse $srcPath -Destination $destPath -passThru).count
ArcSet
  • 6,518
  • 1
  • 20
  • 34
1

If you use -Verbose and redirect/capture the verbose stream, then you can compute the files copied from the verbose messages. This would look like

# run copy with -Verbose and capture stream 4 which is the verbose stream
$vml = Copy-Item -Recurse $srcPath -Destination $destPath -Verbose 4>&1
# Find all the messages with 'Copy File' in them
($vml -match "Copy File").Count

You probably also want to capture any errors using -ErrorVariable copyErrors.

Bruce Payette
  • 2,511
  • 10
  • 8