Copy files and folders recursively in PowerShell

This computer tutorial shows how to copy files and folders recursively in PowerShell, using the Windows Copy-Item command. It allows you to copy a large number of files and folders in a single command.

Windows PowerShell is a shell that allows users to execute commands written in the Microsoft PowerShell scripting language.

The user can run PS scripts on Windows, 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.

Copy files and folders recursively with a PowerShell script

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.

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.

Recursive copy of files and folders from a source folder in PowerShell

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
Show all files recursively in PowerShell with Get-ChildItem command
Show all files recursively in PowerShell with Get-ChildItem command

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 and folders recursively in PowerShell with Copy-Item
Copy files and folders recursively in PowerShell with Copy-Item

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.

Copy files recursively and force overwrite in the target folder

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 and folders with PowerShell recursively and overwrite the target
Copy files and folders with PowerShell recursively and overwrite the target

Selecting specific file type extensions

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"

Exclude files from the copy with PowerShell

Finally, the script offers an option to exclude certain directories from the copy.

To exclude particular 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 make the same recursive copies of files and folders with the MS-DOS xcopy command.

Be the first to comment

Leave a Reply

Your email address will not be published.


*