33

I am using the PowerShell Copy-Item command to copy a directory with files to another location.

I want to display all the files on the console that are getting copied so that I know the status of the copy command.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
tusharmath
  • 10,622
  • 12
  • 56
  • 83

4 Answers4

52

If you just want to see that in console, use the -verbose switch:

copy-item -path $from -destination $to -verbose

If you want to get a list of files or directories:

$files = copy-item -path $from -destination $to -passthru | ?{$_ -is [system.io.fileinfo]}
cxw
  • 16,685
  • 2
  • 45
  • 81
Jackie
  • 2,476
  • 17
  • 20
  • 1
    Another way for testing if an item is a file is to use `? {!$_.PSIsContainer}`. Also, in V3 you can do this `$files = copy-item $from $to -verbose 4>&1` – Keith Hill Dec 11 '12 at 15:48
  • "-verbose" works fine. Thank you. Too bad it's not in the documentation. – n-develop Jan 07 '21 at 07:22
  • 3
    `-Verbose` reports a whole lot of extra information, is there a way to tell it to just output the names of the files copied? – yoyo Jan 15 '21 at 01:17
5
$source=ls c:\temp *.*
$i=1
$source| %{
    [int]$percent = $i / $source.count * 100
    Write-Progress -Activity "Copying ... ($percent %)" -status $_  -PercentComplete $percent -verbose
    copy $_.fullName -Destination c:\test 
    $i++
}
Loïc MICHEL
  • 24,935
  • 9
  • 74
  • 103
3

If you want to directly output the filenames, you can do it this way:

With Path

Copy-Item -Path $from -Destination $to –PassThru | ForEach-Object { Write-Host $_.FullName }

FileName Only

Copy-Item -Path $from -Destination $to –PassThru | ForEach-Object { Write-Host $_.Name }
Peter Widmer
  • 122
  • 10
2

I suggest to try it this way:

(Copy-Item -Verbose C:\SrcDir\*.* c:\DstDir 4>&1).Message

Here the messages go to the output stream/pipeline rather than the verbose stream/pipeline and so will work more generally such as in TFS task scripts.

Martin Connell
  • 175
  • 2
  • 6