title: powershell v2 - rename a file whose name matches a string containing a wildcard
body: <p>I'm trying to create a script to rename a file that matches a given filename, with a wildcard character. e.g.</p>
  
  <p>I have a folder containing the following files:</p>
  
  <ul>
  <li>201412180900_filename_123.log </li>
  <li>201412181000_filename_123.log</li>
  <li>201412181100_filename_456.log </li>
  <li>filename_789.log</li>
  </ul>
  
  <p>I want to scan through this folder, and append the current time to the start of any file starting with the word 'filename'</p>
  
  <p>I have the following so far:</p>
  
  <pre><code>$d = Get-Date -format "yyyyMMddHHmm" 
  $dir = "C:\test"
  $file = "filename*.log"
  
  get-childitem -Path "$dir" | where-object { $_.Name -like "$file" } | rename-item -path $_ -newname $d."_".$_.name
  </code></pre>
  
  <p>but it doesn't work. 
  As I see it the individual sections 'should' work from my reading of the documentation, but clearly something is wrong. If someone can point it out it would be appreciated.</p>
  
  <hr>
  
  <p>We're getting closer. It would appear that <code>-path</code> in the <code>rename-item</code> section needs to be <code>$dir$_</code> as <code>$_</code> (seemingly) only contains the filename. (The get-help example suggests it needs to be the full path and filename)</p>
  
  <p>If I take out the rename-item section and replace it with <code>%{write-host $d"_"$_}</code> it gives the correct new filename</p>
  
  <p>However, simply placing this into <code>rename-item</code> section still doesn't update the filename.</p>
  
  <pre><code>rename-item -path $dir$_ -newname $d"_"$_
  </code></pre>
  
  <hr>
  
  <p>SUCCESS</p>
  
  <p>the correct syntax appears to be:</p>
  
  <pre><code>get-childitem -Path "$dir" | where-object { $_.Name -like "$file" } | %{rename-item -path $dir$_ -newname $d"_"$_}
  </code></pre>
  
  <p>The missing element was the <code>%{ ... }</code> surrounding the <code>rename-item</code> section which enabled me to reference <code>$_</code></p>