Copy files or group of files
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 # Ensure target base folder exist before copy New-Item -Path "c:\Temp\Target\baseFolder\" -ItemType Directory -Force -Verbose # copy one file -_- copy-item -Path "c:\Temp\Source\baseFolder\TextFileRoot1.txt" -Destination "c:\Temp\Target\baseFolder\" -Verbose # copy one file forcing overwrite without prompt -_- copy-item -Path "c:\Temp\Source\baseFolder\TextFileRoot2.txt" -Destination "c:\Temp\Target\baseFolder\" -Verbose -Force -Confirm:$false # copy only *.Log1 and *.Log3 files -_- copy-item -Path "c:\Temp\Source\baseFolder\*" -Include "*.Log1","*.Log3" -Destination "c:\Temp\Target\baseFolder\" -Verbose
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 files on different scenarios # File Management - Copy Files -_- # PowerShellExamples.com # Preparing example source folder/files remove-item -Path "c:\Temp\Source\" -Force -Recurse -Confirm:$false -ErrorAction SilentlyContinue | out-null remove-item -Path "c:\Temp\Target\" -Force -Recurse -Confirm:$false -ErrorAction SilentlyContinue | out-null New-Item -Path "c:\Temp\" -ItemType Directory -Force | out-null New-Item -Path "c:\Temp\Source\baseFolder\" -ItemType Directory -Force| out-null New-Item -Path "c:\Temp\Target\" -ItemType Directory -Force| out-null # Create Text File -_- New-Item "c:\Temp\Source\baseFolder\TextFileRoot1.txt" -ItemType File -Value "TextFileRoot1"| out-null New-Item "c:\Temp\Source\baseFolder\TextFileRoot2.txt" -ItemType File -Value "TextFileRoot2"| out-null New-Item "c:\Temp\Source\baseFolder\LogFileRoot.log1" -ItemType File -Value "LogFileRoot"| out-null New-Item "c:\Temp\Source\baseFolder\LogFileRoot.log2" -ItemType File -Value "LogFileRoot"| out-null New-Item "c:\Temp\Source\baseFolder\LogFileRoot.log3" -ItemType File -Value "LogFileRoot"| out-null Write-Host "Showing all source files" -ForegroundColor DarkCyan Get-ChildItem -Path "c:\Temp\Source\baseFolder\*" -Recurse | select-object -Property FullName | out-string | Write-Host # Ensure target base folder exist before copy New-Item -Path "c:\Temp\Target\baseFolder\" -ItemType Directory -Force -Verbose # Copy one file -_- Write-Host "Copy one file -_-" -ForegroundColor Green copy-item -Path "c:\Temp\Source\baseFolder\TextFileRoot1.txt" -Destination "c:\Temp\Target\baseFolder\" -Verbose Write-Host "Showing copied file" -ForegroundColor DarkCyan Get-Item -Path "c:\Temp\Target\baseFolder\TextFileRoot1.txt" | select-object -Property FullName | out-string | Write-Host # Copy one file forcing overwrite without prompt -_- Write-Host "Copy one file forcing overwrite without prompt -_-" -ForegroundColor Green copy-item -Path "c:\Temp\Source\baseFolder\TextFileRoot2.txt" -Destination "c:\Temp\Target\baseFolder\" -Verbose -Force -Confirm:$false Write-Host "Showing copied file without prompt" -ForegroundColor DarkCyan Get-Item -Path "c:\Temp\Target\baseFolder\TextFileRoot2.txt" | select-object -Property FullName | out-string | Write-Host # Copy only *.Log1 and *.Log3 files -_- Write-Host "Copy only *.Log1 and *.Log3 files -_-" -ForegroundColor Green copy-item -Path "c:\Temp\Source\baseFolder\*" -Include "*.Log1","*.Log3" -Destination "c:\Temp\Target\baseFolder\" -Verbose Write-Host "Showing additon of copied files from filtered extensions *.Log1,*.Log3" -ForegroundColor DarkCyan Get-ChildItem -Path "c:\Temp\Target\baseFolder\*" -Recurse | select-object -Property FullName | out-string | Write-Host
PS C:\> Showing all source files PS C:\Users\cesar> FullName -------- C:\Temp\Source\baseFolder\LogFileRoot.log1 C:\Temp\Source\baseFolder\LogFileRoot.log2 C:\Temp\Source\baseFolder\LogFileRoot.log3 C:\Temp\Source\baseFolder\TextFileRoot1.txt C:\Temp\Source\baseFolder\TextFileRoot2.txt VERBOSE: Performing the operation "Create Directory" on target "Destination: C:\Temp\Target\baseFolder\". Directory: C:\Temp\Target Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 2/24/2023 1:11 AM baseFolder Copy one file -_- VERBOSE: Performing the operation "Copy File" on target "Item: C:\Temp\Source\baseFolder\TextFileRoot1.txt Destination: C:\Temp\Target\baseFolder\TextFileRoot1.txt". Showing copied file FullName -------- C:\Temp\Target\baseFolder\TextFileRoot1.txt Copy one file forcing overwrite without prompt -_- VERBOSE: Performing the operation "Copy File" on target "Item: C:\Temp\Source\baseFolder\TextFileRoot2.txt Destination: C:\Temp\Target\baseFolder\TextFileRoot2.txt". Showing copied file without prompt FullName -------- C:\Temp\Target\baseFolder\TextFileRoot2.txt Copy only *.Log1 and *.Log3 files -_- VERBOSE: Performing the operation "Copy File" on target "Item: C:\Temp\Source\baseFolder\LogFileRoot.log1 Destination: C:\Temp\Target\baseFolder\LogFileRoot.log1". VERBOSE: Performing the operation "Copy File" on target "Item: C:\Temp\Source\baseFolder\LogFileRoot.log3 Destination: C:\Temp\Target\baseFolder\LogFileRoot.log3". Showing additon of copied files from filtered extensions *.Log1,*.Log3 FullName -------- C:\Temp\Target\baseFolder\LogFileRoot.log1 C:\Temp\Target\baseFolder\LogFileRoot.log3 C:\Temp\Target\baseFolder\TextFileRoot1.txt C:\Temp\Target\baseFolder\TextFileRoot2.txt