firekid2018
firekid2018

Reputation: 145

Powershell copy file/folder based on keyword

I want to copy folder which match with the keyword. however i want powershell read the keyword from starting point. i added my script below

if any folder name contain test at the start, script will copy the folder. but it's coping all folder even if "Test" keyword is available in the middle name. like if there is two folder

"This.is.a.Test.Folder" "Test.this.is.a.Folder"

I want powershell copy only "Test.this.is.a.Folder"

any help please

$dest = "D:\2";
$include= @("*Test*")
Get-ChildItem $source -recurse -Force -Verbose -include $include | copy-Item -Destination {Join-Path $dest $_.FullName.Substring($source.length)}```


Upvotes: 0

Views: 269

Answers (1)

Santiago Squarzon
Santiago Squarzon

Reputation: 60045

Your wildcard is meant to capture anything that contains the word Test in this case.

  • If you want to specifically start with the word Test followed by anything: Test*
  • Contrary, anything that ends with the word Test would be: *Test
$include = @( "Test*" )
Get-ChildItem $source -Include $include -Recurse -Force -Verbose |
Copy-Item -Destination {
   Join-Path $dest -ChildPath $_.FullName.Substring($source.length)
}

Note, that you can use -File to filter only files and -Directory to filter only folders.

Upvotes: 1

Related Questions