title: Powershell loop through folders, create file in each folder
body: <p>I'm trying to recursively loop through all the folders in a directory, then do something within each directory. Create a text file, for example.</p>
  
  <p>I can get all the subfolder into a variable like so:</p>
  
  <pre><code>$folders = Get-ChildItem -Recurse | ?{ $_.PSIsContainer }
  </code></pre>
  
  <p>However, I can't seem to create a file within each subdirectory - the files are instead created in the top directory. This doesn't work:</p>
  
  <pre><code>$folders | ForEach-Object { New-Item -ItemType file -Name "$_.txt" -path $_}
  </code></pre>
  
  <p>That throws up an error that the directory is not found or that the path is too long.</p>
  
  <p>How do I do something in each subdirectory of nested folders?</p>