This one-liner PowerShell command copies files matching specified patterns from all subfolders to a destination folder:
gci -r -file $patterns | cp -dest $d -force
-
Set your file patterns:
$patterns = "*.txt","*.log"
-
Set your destination folder:
$d = "C:\DestinationFolder"
-
Run the command.
- Ensure the destination folder is not within the source folder structure to avoid infinite loops.
- This command overwrites existing files in the destination folder.
gci
(Get-ChildItem) recursively finds files matching the patterns.cp
(Copy-Item) copies the files to the destination.-force
overwrites existing files without prompting.
Happy copying!