Copy files recursively in PowerShell including subfolders

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

The Copy-Item command allows you to copy a large number of files and folders recursively, in a single command. PowerShell is a shell used to execute commands written in the Windows PowerShell scripting language. 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 to copy files recursively

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 cmdlet for copying files is called Copy-Item.

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.

Windows PowerShell is designed for system administration, but it can also be used for other purposes. For example, it can be used for tasks such as recursively copying multiple files or folders from one location to another.

1.1 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. To begin, 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

1.2 Copy files recursively from source folder to target with absolute paths

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

1.3 Check the target folder after the copy

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.

2. 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

3. 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"

4. 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"

Still with Windows commands, it is also possible to perform the same recursive copy of files and folders with the DOS xcopy command.

2 thoughts on “Copy files recursively in PowerShell including subfolders”

  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.

    1. 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 Comment

Your email address will not be published. Required fields are marked *

Scroll to Top