How to copy files recursively in PowerShell using Copy-Item?

This Microsoft tutorial explains how to copy files and folders recursively in PowerShell, using the Windows Copy-Item command.

The Copy-Item command allows you to copy Windows files recursively in PowerShell, using a single command. The user can run scripts on Windows servers or clients, which are similar to the batch files of older versions of Windows, i.e. MS-DOS commands. Note that both DOS commands and PowerShell are available on Windows 10 and Windows 11 for example as well as in previous Windows versions.

1. PowerShell scripts and the Copy-Item command

First of all, Windows PowerShell is a task-based command-line shell and scripting language that is built on the .NET Framework. Windows PowerShell includes a cmdlet (command-line tool) for copying files. Cmdlets are like small commands that can be combined to perform tasks such as copying files.

The script language will help you reduce the time it takes to copy files, automate tasks, manage system configurations, and more. The Copy-Item cmdlet can be used to copy one or more items from one location to another, such as from a file system folder to a CD drive, or from one folder in a file system tree to another folder in the same tree.

2. List recursively files, folders, and subfolders before the copy

The script starts by defining the location of the source folder and then copies all files and folders from that location to the destination folder. For example, here is a simple, practical case, a C:\WORK\Folder source folder with two subfolders:

  • Folder contains
    • file (1).txt
    • file (2).txt
    • file (3).txt
    • The SubFolder_1 folder contains
      • file (4).txt
      • file (5).txt
      • file (6).txt

The goal is to copy all the files and the tree structure into the target folder: C:\WORK\Target

Get-ChildItem C:\WORK\Folder -Recurse
List all files recursively with the PowerShell Get-ChildItem command
List all files recursively with the PowerShell Get-ChildItem command

3. Copy files recursively from source folder to target with absolute paths

In this third section, we present this time a script to copy all files in folders and subfolders with the Copy-Item command and the -Recurse option.

Copy-Item -Path C:\WORK\Folder\ -Destination C:\WORK\Target -Recurse
Copy files recursively in PowerShell with Copy-Item
Copy files recursively in PowerShell with Copy-Item

4. Check the target folder after the copy

It is also possible to check that the files are copied to the target folder:

Get-ChildItem C:\WORK\Target -Recurse
Display files and folders in the target folder recursively in PowerShell
Display files and folders in the target folder recursively in PowerShell

The script has successfully copied all files and folders from the WORK folder, including the folder named Folder.

5. Copy files recursively and force overwrite of the target

To force the copy if one or more folders or files already exist in the target folder, use the -Force option.

Copy-Item -Path C:\WORK\Folder\ -Destination C:\WORK\Target -Recurse -Force
Copy files recursively in PowerShell with Copy-Item and overwrite the target
Copy files recursively in PowerShell with Copy-Item and overwrite the target

6. Selecting specific file type extensions to copy

The script also provides an option to copy only specific file types, such as *.docx or *.jpg, or a specific file with a given name. For example, to force the copy of TXT files only and ignore all other extensions, use this script:

Copy-Item -Path C:\WORK\Folder\ -Destination C:\WORK\Target -Recurse -Force -Filter "*.txt"

7. Exclude files from the PowerShell copy

Finally, the script offers an option to exclude certain directories from the copy. To exclude file extensions, use the PS -Exclude option. In this example, the copy is recursive and copies all files except Word files with a Docx extension.

Copy-Item -Path C:\WORK\Folder\ -Destination C:\WORK\Target -Recurse -Exclude "*.docx"

Conclusion on recursive file copy using PowerShell

To conclude, this article details how to use PowerShell’s Copy-Item command for file management. It teaches readers to list files recursively using the Get-ChildItem command, copy files with the -Recurse option, verify copied content in the target location, and overwrite existing files with the -Force option. Still with Windows commands, it is also possible to perform the same recursive copy of files and folders with the DOS xcopy command.

2 Comments

  1. Thanks for your Help. This will greatly help keep my Backup Disk up to date.
    I hate file history. With your help I can now just Create a new director and run the script. Much appreciated.

    • We are happy it helped you! Do not hesitate to publish more comments or suggestions and to share the article.
      Expert-Only team

Leave a Reply

Your email address will not be published.


*