<<< Back to Search

File Management - Copy Folders (Copy-Item)

Copy Folders and files on different scenarios

Snippets

Adapt and Copy following snippets on your code/context. At the end of the article there is a detailed explanation within a Practical sample.

# Objects - Initialize from CSV
# Snippets
# www.PowerShellExamples.com


# copy only folder (without files) -_-
copy-item -Path "c:\Temp\Source_2\baseFolder\" -Destination "c:\Temp\Target_2\" -Container -Verbose

# Copy only folder forcing overwrite without prompt -_-
copy-item -Path "c:\Temp\Source_2\baseFolder_2\" -Destination "c:\Temp\Target_2\" -Container -Verbose -Force -Confirm:$false

# Copy folder root elements (exclude subfolder files - no recursive -) -_-
copy-item -Path "c:\Temp\Source_2\baseFolder\*" -Destination "c:\Temp\Target_2\baseFolder\" -Verbose

# Copy folders and files recursively -_-
#   Ensure target base folder exist before copy
New-Item -Path "c:\Temp\Target_2\baseFolder_3\" -ItemType Directory -Force -Verbose
copy-item -Path "c:\Temp\Source_2\baseFolder\" -Destination "c:\Temp\Target_2\baseFolder_3\" -recurse -Verbose 

# Copy folders and files recursively filtering with a pattern -_-
#   parameter -Filter not allow multiple patterns (i.e. *.log1,*.log2) but works ok with recursive output for single pattern
#     *(Parameter -Include indeed allow multiple patterns but not respect folder structure on the target,
#                          Useful to flatten folder/files trees - See next example)
# Ensure target base folder exist before copy
New-Item -Path "c:\Temp\Target_2\baseFolder_4\" -ItemType Directory -Force -Verbose | out-null
copy-item -path "c:\Temp\Source_2\baseFolder\" -Filter "*.log1" -Destination "c:\Temp\Target_2\baseFolder_4\" -recurse -verbose

# Copy Files and sub folders allowing filtering, get them recursively but flatten on target -_-
# Ensure target base folder exist before copy
New-Item -Path "c:\Temp\Target_2\baseFolder_5\" -ItemType Directory -Force -Verbose | out-null
Get-ChildItem -Path "c:\Temp\Source_2\baseFolder\*" -Include "*.log1","*.log3" -Recurse | Copy-Item -Destination "c:\Temp\Target_2\baseFolder_5\" -Verbose


Practical Sample

Copy below code in a power shell session and execute to see commands in action.
You can find expected results at the end of the article.

# Sample : copy folders on different scenarios
# File Management - Copy Files -_-
# PowerShellExamples.com

# Preparing example source folder/files
remove-item -Path "c:\Temp\Source_2\" -Force -Recurse -Confirm:$false | out-null
remove-item -Path "c:\Temp\Target_2\" -Force -Recurse -Confirm:$false | out-null

New-Item -Path "c:\Temp\" -ItemType Directory -Force | out-null
New-Item -Path "c:\Temp\Source_2\baseFolder\" -ItemType Directory -Force| out-null
New-Item -Path "c:\Temp\Source_2\baseFolder\SubFolder\" -ItemType Directory -Force| out-null
New-Item -Path "c:\Temp\Source_2\baseFolder_2\" -ItemType Directory -Force| out-null
New-Item -Path "c:\Temp\Target_2\" -ItemType Directory -Force| out-null
# Create Text Files -_-
New-Item "c:\Temp\Source_2\baseFolder\TextFileRoot1.txt" -ItemType File -Value "TextFileRoot1"| out-null
New-Item "c:\Temp\Source_2\baseFolder\TextFileRoot2.txt" -ItemType File -Value "TextFileRoot2"| out-null
New-Item "c:\Temp\Source_2\baseFolder\LogFileRoot.log1" -ItemType File -Value "LogFileRoot"| out-null
New-Item "c:\Temp\Source_2\baseFolder\LogFileRoot.log2" -ItemType File -Value "LogFileRoot"| out-null
New-Item "c:\Temp\Source_2\baseFolder\LogFileRoot.log3" -ItemType File -Value "LogFileRoot"| out-null
New-Item "c:\Temp\Source_2\baseFolder\SubFolder\TextFileSub1.txt" -ItemType File -Value "TextFileSub1"| out-null
New-Item "c:\Temp\Source_2\baseFolder\SubFolder\TextFileSub2.txt" -ItemType File -Value "TextFileSub2"| out-null
New-Item "c:\Temp\Source_2\baseFolder\SubFolder\LogFileSub.log1" -ItemType File -Value "LogFileSub1"| out-null
New-Item "c:\Temp\Source_2\baseFolder\SubFolder\LogFileSub.log2" -ItemType File -Value "LogFileSub2"| out-null
New-Item "c:\Temp\Source_2\baseFolder\SubFolder\LogFileSub.log3" -ItemType File -Value "LogFileSub3"| out-null


Write-Host "Showing all source folder and files" -ForegroundColor DarkCyan
Get-ChildItem -Path "c:\Temp\Source_2\*" -Recurse | select-object -Property FullName | out-string | Write-Host

# copy only folder (without files) -_-
Write-Host "Copy only folder (without files) -_-" -ForegroundColor Green
copy-item -Path "c:\Temp\Source_2\baseFolder\" -Destination "c:\Temp\Target_2\" -Container -Verbose

Write-Host "Showing copied folder (No files expected on target): \baseFolder\" -ForegroundColor DarkCyan
Get-ChildItem -Path "c:\Temp\Target_2\*" | select-object -Property FullName | out-string | Write-Host


# Copy only folder forcing overwrite without prompt -_-
Write-Host "Copy only folder forcing overwrite without prompt -_-" -ForegroundColor Green
copy-item -Path "c:\Temp\Source_2\baseFolder_2\" -Destination "c:\Temp\Target_2\" -Container -Verbose -Force -Confirm:$false

Write-Host "Showing copied file without prompt: \baseFolder_2\" -ForegroundColor DarkCyan
Get-ChildItem -Path "c:\Temp\Target_2\*" | select-object -Property FullName | out-string | Write-Host


# Copy folder root elements (exclude subfolder files - no recursive -) -_-
Write-Host "Copy folder root elements (exclude subfolder files - no recursive -) -_-" -ForegroundColor Green
copy-item -Path "c:\Temp\Source_2\baseFolder\*" -Destination "c:\Temp\Target_2\baseFolder\" -Verbose

Write-Host "Showing copied folder including root elements" -ForegroundColor DarkCyan
Get-ChildItem -Path "c:\Temp\Target_2\baseFolder\*" -Recurse | select-object -Property FullName | out-string | Write-Host


# Copy folders and files recursively -_-
Write-Host "Copy folders and files recursively -_-" -ForegroundColor Green
# ensure target base folder exist before copy
New-Item -Path "c:\Temp\Target_2\baseFolder_3\" -ItemType Directory -Force -Verbose
copy-item -Path "c:\Temp\Source_2\baseFolder\" -Destination "c:\Temp\Target_2\baseFolder_3\" -recurse -Verbose 

Write-Host "Showing folder/files tree on target_2: \baseFolder_3\" -ForegroundColor DarkCyan
Get-ChildItem -Path "c:\Temp\Target_2\baseFolder_3\*" -Recurse | select-object -Property FullName | out-string | Write-Host


# Copy folders and files recursively filtering with a pattern -_-
#   parameter -Filter not allow multiple patterns (i.e. *.log1,*.log2) but works ok with recursive output for single pattern
#     *(Parameter -Include indeed allow multiple patterns but not respect folder structure on the target,
#                          Useful to flatten folder/files trees - See next example)

# ensure target base folder exist before copy
New-Item -Path "c:\Temp\Target_2\baseFolder_4\" -ItemType Directory -Force -Verbose | out-null

Write-Host "Copy folders and files recursively filtering with a pattern -_-" -ForegroundColor Green
copy-item -path "c:\Temp\Source_2\baseFolder\" -Filter "*.log1" -Destination "c:\Temp\Target_2\baseFolder_4\" -recurse -verbose

Write-Host "Showing folder/files tree on target_2: \baseFolder_4\" -ForegroundColor DarkCyan
Get-ChildItem -Path "c:\Temp\Target_2\baseFolder_4\*" -Recurse | select-object -Property FullName | out-string | Write-Host


# Copy Files and sub folders allowing filtering, get them recursively but flatten on target -_-
#     This command get files with Get-ChildItem and Pipe the object to Copy-Item command

# Ensure target base folder exist before copy
New-Item -Path "c:\Temp\Target_2\baseFolder_5\" -ItemType Directory -Force -Verbose | out-null

Write-Host "Copy Files and sub folders allowing filtering, get them recursively but flatten on target -_-" -ForegroundColor Green
Get-ChildItem -Path "c:\Temp\Source_2\baseFolder\*" -Include "*.log1","*.log3" -Recurse | Copy-Item -Destination "c:\Temp\Target_2\baseFolder_5\" -Verbose

Write-Host "Showing folder/files flatten on target_2: \baseFolder_5\" -ForegroundColor DarkCyan
Get-ChildItem -Path "c:\Temp\Target_2\baseFolder_5\*" -Recurse | select-object -Property FullName | out-string | Write-Host



Result

PS C:\>

Showing all source folder and files

FullName
--------
C:\Temp\Source_2\baseFolder\SubFolder
C:\Temp\Source_2\baseFolder\SubFolder\LogFileSub.log1
C:\Temp\Source_2\baseFolder\SubFolder\LogFileSub.log2
C:\Temp\Source_2\baseFolder\SubFolder\LogFileSub.log3
C:\Temp\Source_2\baseFolder\SubFolder\TextFileSub1.txt
C:\Temp\Source_2\baseFolder\SubFolder\TextFileSub2.txt
C:\Temp\Source_2\baseFolder\LogFileRoot.log1
C:\Temp\Source_2\baseFolder\LogFileRoot.log2
C:\Temp\Source_2\baseFolder\LogFileRoot.log3
C:\Temp\Source_2\baseFolder\TextFileRoot1.txt
C:\Temp\Source_2\baseFolder\TextFileRoot2.txt



Copy only folder (without files) -_-
VERBOSE: Performing the operation "Copy Directory" on target "Item: C:\Temp\Source_2\baseFolder\ Destination: C:\Temp\Target_2\baseFolder".
VERBOSE: Performing the operation "Create Directory" on target "Destination: C:\Temp\Target_2\baseFolder".
Showing copied folder (No files expected on target): \baseFolder
FullName
--------
C:\Temp\Target_2\baseFolder



Copy only folder forcing overwrite without prompt -_-
VERBOSE: Performing the operation "Copy Directory" on target "Item: C:\Temp\Source_2\baseFolder_2\ Destination: C:\Temp\Target_2\baseFolder_2".
VERBOSE: Performing the operation "Create Directory" on target "Destination: C:\Temp\Target_2\baseFolder_2".
Showing copied file without prompt: \baseFolder_2
FullName
--------
C:\Temp\Target_2\baseFolder
C:\Temp\Target_2\baseFolder_2



Copy folder root elements (exclude subfolder files - no recursive -) -_-
VERBOSE: Performing the operation "Copy Directory" on target "Item: C:\Temp\Source_2\baseFolder\SubFolder Destination: C:\Temp\Target_2\baseFolder\SubFolder".
VERBOSE: Performing the operation "Create Directory" on target "Destination: C:\Temp\Target_2\baseFolder\SubFolder".
VERBOSE: Performing the operation "Copy File" on target "Item: C:\Temp\Source_2\baseFolder\LogFileRoot.log1 Destination: C:\Temp\Target_2\baseFolder\LogFileRoot.log1".
VERBOSE: Performing the operation "Copy File" on target "Item: C:\Temp\Source_2\baseFolder\LogFileRoot.log2 Destination: C:\Temp\Target_2\baseFolder\LogFileRoot.log2".
VERBOSE: Performing the operation "Copy File" on target "Item: C:\Temp\Source_2\baseFolder\LogFileRoot.log3 Destination: C:\Temp\Target_2\baseFolder\LogFileRoot.log3".
VERBOSE: Performing the operation "Copy File" on target "Item: C:\Temp\Source_2\baseFolder\TextFileRoot1.txt Destination: C:\Temp\Target_2\baseFolder\TextFileRoot1.txt".
VERBOSE: Performing the operation "Copy File" on target "Item: C:\Temp\Source_2\baseFolder\TextFileRoot2.txt Destination: C:\Temp\Target_2\baseFolder\TextFileRoot2.txt".
Showing copied folder including root elements

FullName
--------
C:\Temp\Target_2\baseFolder\SubFolder
C:\Temp\Target_2\baseFolder\LogFileRoot.log1
C:\Temp\Target_2\baseFolder\LogFileRoot.log2
C:\Temp\Target_2\baseFolder\LogFileRoot.log3
C:\Temp\Target_2\baseFolder\TextFileRoot1.txt
C:\Temp\Target_2\baseFolder\TextFileRoot2.txt



Copy folders and files recursively -_-
VERBOSE: Performing the operation "Create Directory" on target "Destination: C:\Temp\Target_2\baseFolder_3\".


    Directory: C:\Temp\Target_2


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         2/24/2023   1:12 AM                baseFolder_3
VERBOSE: Performing the operation "Copy Directory" on target "Item: C:\Temp\Source_2\baseFolder\ Destination: C:\Temp\Target_2\baseFolder_3\baseFolder".
VERBOSE: Performing the operation "Create Directory" on target "Destination: C:\Temp\Target_2\baseFolder_3\baseFolder".
VERBOSE: Performing the operation "Copy File" on target "Item: C:\Temp\Source_2\baseFolder\LogFileRoot.log1 Destination: C:\Temp\Target_2\baseFolder_3\baseFolder\LogFileRoot.log1".
VERBOSE: Performing the operation "Copy File" on target "Item: C:\Temp\Source_2\baseFolder\LogFileRoot.log2 Destination: C:\Temp\Target_2\baseFolder_3\baseFolder\LogFileRoot.log2".
VERBOSE: Performing the operation "Copy File" on target "Item: C:\Temp\Source_2\baseFolder\LogFileRoot.log3 Destination: C:\Temp\Target_2\baseFolder_3\baseFolder\LogFileRoot.log3".
VERBOSE: Performing the operation "Copy File" on target "Item: C:\Temp\Source_2\baseFolder\TextFileRoot1.txt Destination: C:\Temp\Target_2\baseFolder_3\baseFolder\TextFileRoot1.txt".
VERBOSE: Performing the operation "Copy File" on target "Item: C:\Temp\Source_2\baseFolder\TextFileRoot2.txt Destination: C:\Temp\Target_2\baseFolder_3\baseFolder\TextFileRoot2.txt".
VERBOSE: Performing the operation "Copy Directory" on target "Item: C:\Temp\Source_2\baseFolder\SubFolder Destination: C:\Temp\Target_2\baseFolder_3\baseFolder\SubFolder".
VERBOSE: Performing the operation "Create Directory" on target "Destination: C:\Temp\Target_2\baseFolder_3\baseFolder\SubFolder".
VERBOSE: Performing the operation "Copy File" on target "Item: C:\Temp\Source_2\baseFolder\SubFolder\LogFileSub.log1 Destination: C:\Temp\Target_2\baseFolder_3\baseFolder\SubFolder\LogFileSub.log1".
VERBOSE: Performing the operation "Copy File" on target "Item: C:\Temp\Source_2\baseFolder\SubFolder\LogFileSub.log2 Destination: C:\Temp\Target_2\baseFolder_3\baseFolder\SubFolder\LogFileSub.log2".
VERBOSE: Performing the operation "Copy File" on target "Item: C:\Temp\Source_2\baseFolder\SubFolder\LogFileSub.log3 Destination: C:\Temp\Target_2\baseFolder_3\baseFolder\SubFolder\LogFileSub.log3".
VERBOSE: Performing the operation "Copy File" on target "Item: C:\Temp\Source_2\baseFolder\SubFolder\TextFileSub1.txt Destination: C:\Temp\Target_2\baseFolder_3\baseFolder\SubFolder\TextFileSub1.txt".
VERBOSE: Performing the operation "Copy File" on target "Item: C:\Temp\Source_2\baseFolder\SubFolder\TextFileSub2.txt Destination: C:\Temp\Target_2\baseFolder_3\baseFolder\SubFolder\TextFileSub2.txt".
Showing folder/files tree on target_2: \baseFolder_3
FullName
--------
C:\Temp\Target_2\baseFolder_3\baseFolder\SubFolder
C:\Temp\Target_2\baseFolder_3\baseFolder\SubFolder\LogFileSub.log1
C:\Temp\Target_2\baseFolder_3\baseFolder\SubFolder\LogFileSub.log2
C:\Temp\Target_2\baseFolder_3\baseFolder\SubFolder\LogFileSub.log3
C:\Temp\Target_2\baseFolder_3\baseFolder\SubFolder\TextFileSub1.txt
C:\Temp\Target_2\baseFolder_3\baseFolder\SubFolder\TextFileSub2.txt
C:\Temp\Target_2\baseFolder_3\baseFolder\LogFileRoot.log1
C:\Temp\Target_2\baseFolder_3\baseFolder\LogFileRoot.log2
C:\Temp\Target_2\baseFolder_3\baseFolder\LogFileRoot.log3
C:\Temp\Target_2\baseFolder_3\baseFolder\TextFileRoot1.txt
C:\Temp\Target_2\baseFolder_3\baseFolder\TextFileRoot2.txt



VERBOSE: Performing the operation "Create Directory" on target "Destination: C:\Temp\Target_2\baseFolder_4\".
Copy folders and files recursively filtering with a pattern -_-
VERBOSE: Performing the operation "Copy Directory" on target "Item: C:\Temp\Source_2\baseFolder\ Destination: C:\Temp\Target_2\baseFolder_4\baseFolder".
VERBOSE: Performing the operation "Create Directory" on target "Destination: C:\Temp\Target_2\baseFolder_4\baseFolder".
VERBOSE: Performing the operation "Copy File" on target "Item: C:\Temp\Source_2\baseFolder\LogFileRoot.log1 Destination: C:\Temp\Target_2\baseFolder_4\baseFolder\LogFileRoot.log1".
VERBOSE: Performing the operation "Copy Directory" on target "Item: C:\Temp\Source_2\baseFolder\SubFolder Destination: C:\Temp\Target_2\baseFolder_4\baseFolder\SubFolder".
VERBOSE: Performing the operation "Create Directory" on target "Destination: C:\Temp\Target_2\baseFolder_4\baseFolder\SubFolder".
VERBOSE: Performing the operation "Copy File" on target "Item: C:\Temp\Source_2\baseFolder\SubFolder\LogFileSub.log1 Destination: C:\Temp\Target_2\baseFolder_4\baseFolder\SubFolder\LogFileSub.log1".
Showing folder/files tree on target_2: \baseFolder_4
FullName
--------
C:\Temp\Target_2\baseFolder_4\baseFolder\SubFolder
C:\Temp\Target_2\baseFolder_4\baseFolder\SubFolder\LogFileSub.log1
C:\Temp\Target_2\baseFolder_4\baseFolder\LogFileRoot.log1



VERBOSE: Performing the operation "Create Directory" on target "Destination: C:\Temp\Target_2\baseFolder_5\".
Copy Files and sub folders allowing filtering, get them recursively but flatten on target -_-
VERBOSE: Performing the operation "Copy File" on target "Item: C:\Temp\Source_2\baseFolder\SubFolder\LogFileSub.log1 Destination: C:\Temp\Target_2\baseFolder_5\LogFileSub.log1".
VERBOSE: Performing the operation "Copy File" on target "Item: C:\Temp\Source_2\baseFolder\SubFolder\LogFileSub.log3 Destination: C:\Temp\Target_2\baseFolder_5\LogFileSub.log3".
VERBOSE: Performing the operation "Copy File" on target "Item: C:\Temp\Source_2\baseFolder\LogFileRoot.log1 Destination: C:\Temp\Target_2\baseFolder_5\LogFileRoot.log1".
VERBOSE: Performing the operation "Copy File" on target "Item: C:\Temp\Source_2\baseFolder\LogFileRoot.log3 Destination: C:\Temp\Target_2\baseFolder_5\LogFileRoot.log3".
Showing folder/files flatten on target_2: \baseFolder_5
FullName
--------
C:\Temp\Target_2\baseFolder_5\LogFileRoot.log1
C:\Temp\Target_2\baseFolder_5\LogFileRoot.log3
C:\Temp\Target_2\baseFolder_5\LogFileSub.log1
C:\Temp\Target_2\baseFolder_5\LogFileSub.log3