question

KalaimaniThirupathi-5306 avatar image
0 Votes"
KalaimaniThirupathi-5306 asked AndreasBaumgarten edited

Powershell to copy the folder and files

Dear All,


Currently using the below script to copy the single file but I need to copy folder, subfolder and files also. Can you someone help me with this

and I need to see the progress of the copy




$sourceFiles = Read-Host "Enter The Restored location with file Name"
$rootDestFolder = Read-Host "Enter The User Request Restore Location"
$resuilt =foreach($sourceFile in $sourceFiles){
$filesplit = $sourceFile.split("\")
$splitcount = $filesplit.count
# This example strips the drive letter & first folder ( ex: E:\Subfolder\ ) but appends the rest of the path to the rootDestFolder
$destFile = $rootDestFolder + "\" + $($($sourceFile.split("\")[2..$splitcount]) -join "\")
# Output List of source and dest
Write-Host ""
Write-Host "Source File Location : $sourceFile " -ForegroundColor Green
Write-Host "Destination File Location : $destFile " -ForegroundColor yello
# Create path and file, if they do not already exist
$destPath = Split-Path $destFile
If(!(Test-Path $destPath)) { New-Item $destPath -Type Directory }
If(!(Test-Path $destFile)) { Copy-Item $sourceFile $destFile -recurse }
}

windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

AndreasBaumgarten avatar image
1 Vote"
AndreasBaumgarten answered AndreasBaumgarten edited

Hi @KalaimaniThirupathi-5306 ,

the easiest way to copy folder, subfolder and files is this:

 Copy-Item -Path "C:\sourcefolder" -Destination "e:\targetfolder" -recurse -Force

To get a progess bar you can try the answer from MichalGajda here:
https://social.technet.microsoft.com/Forums/windowsserver/en-US/c957ca7a-088e-40fb-8ce6-23da4d0753bb/progress-bar-for-copied-files-in-powershell?forum=winserverpowershell


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.