Powershell: Copy files and additional files with different name

Powershell Header

This maybe helps some people which need to copy mutiple files. In my example I search for Contoso in files called info_*****.txt I need to copy them and also copy the file data_*****.txt.

$sourceFolder = "E:\temp\source"
$destinationFolder = "E:\temp\folder1"

$files = Get-ChildItem $sourceFolder -Filter *.txt -Recurse | Select-String "Contoso" # Get all Files with Contoso
Write-Host "Files found: " $files.count # Number of files found
foreach ($file in $files){
Get-Childitem $sourceFolder | Where-Object { $_.name -eq $file.filename } | Copy-Item -Destination $destinationFolder # copy all info_*****.txt files
$name = $file.filename -replace "info_", "data_"
Get-Childitem $sourceFolder | Where-Object { $_.name -eq $name } | Copy-Item -Destination $destinationFolder # copy all data_*****.txt files
}

More Infos about Select-String and file copy

Related Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>