PowerShell: Use Copy-Item and Show Files Being Copied


You can use the Copy-Item cmdlet in PowerShell to copy files and folders to a new location.

To show the files that are being copied, you can use one of the following methods:

Method 1: Use Copy-Item with -verbose

$source = "c:\users\bobbi\data1\*"
$destination = "c:\users\bobbi\data2"

Copy-Item -path $source -dest $destination -verbose

This particular example uses the -verbose parameter to output information about the files being copied from the data1 folder into the data2 folder.

Method 2: Use Copy-Item with -passthru

$source = "c:\users\bobbi\data1\*"
$destination = "c:\users\bobbi\data2"

Copy-Item -path $source -dest $destination –passthru |
  ForEach-Object { Write-Host $_.Name }

This particular example uses the -passthru parameter to output only the filenames being copied from the data1 folder into the data2 folder.

The following examples show how to use each method in practice.

Example 1: Use Copy-Item with -verbose

Suppose that we have  folder with the following location:

  • c:\users\bobbi\data1

Suppose that this folder contains three text files and that we would like to copy each of these files to a folder with the following location:

  • c:\users\bobbi\data2

We can use the Copy-Item cmdlet along with the -verbose parameter to copy all three files and display information about the files being copied:

$source = "c:\users\bobbi\data1\*"
$destination = "c:\users\bobbi\data2"

Copy-Item -path $source -dest $destination -verbose

The following screenshot shows how to use this syntax in practice:

PowerShell Copy-Item verbose

The -verbose parameter displays information about each of the three files being copied, including the complete source path and the complete destination path for each file.

Example 2: Use Copy-Item with -passthru

If you’d like to use the Copy-Item cmdlet to copy files from one folder to another and only show the names of the filenames being copied, you can use the -passthru parameter with the following syntax:

$source = "c:\users\bobbi\data1\*"
$destination = "c:\users\bobbi\data2"

Copy-Item -path $source -dest $destination –passthru |
  ForEach-Object { Write-Host $_.Name }

The following screenshot shows how to use this syntax in practice:

PowerShell copy files and only show filenames being copied

Notice that only the names of the three files being copied is shown in the output:

  • coaches.txt
  • managers.txt
  • regions.txt

By using the -passthru parameter we are able to suppress any output from Copy-Item and then by using a ForEach-Object loop we’re able to write only the filename to the output.

Note: You can find the complete documentation for the Copy-Item cmdlet in PowerShell here.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Copy and Rename a File
PowerShell: How to Use Copy-Item and Create New Directory
PowerShell: How to Use Copy-Item and Exclude Folders

Leave a Reply