Skip to content

Instantly share code, notes, and snippets.

@oaustegard
Created September 10, 2024 15:27
Show Gist options
  • Save oaustegard/fd7225aadd30dc8cb9585057d683fe4f to your computer and use it in GitHub Desktop.
Save oaustegard/fd7225aadd30dc8cb9585057d683fe4f to your computer and use it in GitHub Desktop.
Copy files recursively with PowerShell

Quick File Copy in PowerShell

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

Usage

  1. Set your file patterns:

    $patterns = "*.txt","*.log"
  2. Set your destination folder:

    $d = "C:\DestinationFolder"
  3. Run the command.

Notes

  • Ensure the destination folder is not within the source folder structure to avoid infinite loops.
  • This command overwrites existing files in the destination folder.

How it works

  • 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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment