Search code examples
powershellpowershell-2.0powershell-3.0powershell-4.0

Copy a file into a folder and all subfolders


I have a folder 'A' which as 2 subfolders 'A-1' & 'A-2' and 'A-1' has 2 more sub folders 'A-1-1' and 'A-1-2' under it.

I would like to copy a file in all these folders. Below is my powershell script.The problem is the file gets copied to some folders& for other folders I get a 'path not found error' Can you please suggest me what I can do to make it work ?

$folders = Get-ChildItem C:\DestinationFolder\A\ -Directory -Recurse
foreach ($folder in $folders) {
    Copy-Item -Path "C:\Filetocopy\abc.txt" -Destination "C:\DestinationFolder\A\$($folder.name)" -Recurse
}

Solution

  • Maybe you can use this:

    $folders = Get-ChildItem C:\DestinationFolder\A\ -Directory -Recurse
    
    foreach ($folder in $folders) 
    {
        Copy-Item -Path "C:\Filetocopy\abc.txt" -Destination $folder.fullname
    }