title: Foreach command with a folder with many files using a template txt file
body: <p>Using the following code to select help select 1 file from a list of many dated files and then copy to share. Using the following code very kindly provided by fellow Stacker </p>
  
  <pre><code>$pastdays = -7
  
  $pastdate = [datetime]::Now.AddDays($pastdays)
  
  $files = Get-ChildItem "V:\Capacity Manager Size reports\"
  
  $filename = ($files | Where-Object {$_.Name -like "Database_Server1_Userdb1_" + $pastdate.Day  + "_" + $pastdate.Month + "_" + $pastdate.Year+ "*.pdf"})
  
  Copy-Item -Path $filename.FullName "\\Server50\report_Archive"
  </code></pre>
  
  <p>I need to expand this process to run for many files in the folder – each with slightly different names but following a common template to the file name</p>
  
  <p>Original filenames examples:</p>
  
  <pre><code>Database_Server1_UserDB1_30_10_2012_00_20_51
  Database_Server2_UserDB2_30_10_2012_01_20_51
  </code></pre>
  
  <p>Etc</p>
  
  <p>Etc</p>
  
  <p>But im not sure how to best do this in a foreach ?</p>
  
  <p>I was thinking create a template file to load into a variable that holds all the examples of the files to examined before copying</p>
  
  <p>So:</p>
  
  <pre><code>$Report_Template = Get-Content "C:\Powershell\FileTemplate_Name.txt";
  
  FileTemplate_name.txt:
  
  Database_Server1_tempdb_
  
  Database_Server1_UserDB1_
  
  Database_Server1_UserDB2_
  
  Database_Server2_UserDB2_
  
  Database_Server2_UserDB4_
  
  
  $Report_Template = Get-Content "C:\Powershell\QCM_Template_Name.txt";
  
  $pastdays = -7
  
  $pastdate = [datetime]::Now.AddDays($pastdays)
  
  $files = Get-ChildItem "V:\Capacity Manager Size reports\"
  
  foreach($template in $Report_Template)
  
  $filename = ($files | Where-Object {$_.Name -like $template + $pastdate.Day  + "_" + $pastdate.Month + "_" + $pastdate.Year+ "*.pdf"})
  
  Copy-Item -Path $filename.FullName "\\Server50\CapacityManagerReports_Archive\"
  </code></pre>