This maybe helps some people which need to copy multiple 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. In this blog post, I am going to show you how you can use PowerShell to copy files and additional files with a different name using PowerShell.
$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 information about Select-String and file copy. You can find more about Select-String on Microsoft Docs. If you want to know more about PowerShell, check out my blog post on how to install PowerShell 6 and PowerShell 7. If you have any questions, please let me know in the comments.
Tags: Copy Files, file, files, Microsoft, PowerShell, Select-String, Windows, Windows Powershell Last modified: September 13, 2019