title: How to rename files with a conditional statement in PowerShell
body: <p>I am trying to rename a whole lot of files all located in one folder but in different subfolders. The files should be renamed so that their name consists of the foldername + the original file name. I am wondering if you could add a conditional statements so that the file name doesn't change if the file name already contains the folder name. The code below performs the function of renaming the files but doesn't contain the if statement.</p>
  
  <pre><code>dir -recurse | Rename-Item -NewName {$_.Directory.Name + " - " + $_.Name}
  </code></pre>
  
  <p>The code below is an example on how I imagine the code would look:</p>
  
  <pre><code>dir -recurse | if($_.Name -contains $_.Directory.Name) {Rename-Item -NewName {$_.Directory.Name + " - " + $_.Name}}
  </code></pre>