Home > Mobile >  Saving various files in different folders into one single folder using powershell
Saving various files in different folders into one single folder using powershell

Time:05-21

I have a 3 folders here with 3 files.

enter image description here

Within each there is a csv file, I want to save them all into a destination folder without having to open each folder and drag and drop the file into the destination folder.

I attempted this.

$destination = "C:\Desktop\Test"
$sourcefiles = get-childitem -recurse
    
foreach ($file in $sourcefiles)
{
    Copy-Item $file.FullName -Destination "$destination\$file.Name"
}

When I do that, I get the folders copied, which is really cool, but no file.

enter image description here

Any help is appreciated

Looking for something like this...FileA resides in TestA, FileB resides in TestB.. I have several hundred of these and I have to save them into a backup location

enter image description here

CodePudding user response:

You are looking to copy the folders and it's contents to a new destination, so, simply target the folders and then use Copy-Item -Recurse:

Get-ChildItem path\to\testfolders -Directory | Copy-Item -Destination "C:\Desktop\Test" -Recurse
  • Related