title: How to delete empty subfolders with PowerShell?
body: <p>I have a share that is a "junk drawer" for end-users.  They are able to create folders and subfolders as they see fit.  I need to implement a script to delete files created more than 31 days old.  </p>
  
  <p>I have that started with Powershell.  I need to follow up the file deletion script by deleting subfolders that are now empty.  Because of the nesting of subfolders, I need to avoid deleting a subfolder that is empty of files, but has a subfolder below it that contains a file.</p>
  
  <p>For example:</p>
  
  <ul>
  <li><code>FILE3a</code> is 10 days old.  <code>FILE3</code> is 45 days old.</li>
  <li>I want to clean up the structure removing files older than 30 days, and delete empty subfolders.</li>
  </ul>
  
  <pre class="lang-none prettyprint-override"><code>C:\Junk\subfolder1a\subfolder2a\FILE3a
  
  C:\Junk\subfolder1a\subfolder2a\subfolder3a
  
  C:\Junk\subfolder1a\subfolder2B\FILE3b
  </code></pre>
  
  <p>Desired result:</p>
  
  <ul>
  <li>Delete: <code>FILE3b</code>, <code>subfolder2B</code> &amp; <code>subfolder3a</code>.  </li>
  <li>Leave: <code>subfolder1a</code>, <code>subfolder2a</code>, and <code>FILE3a</code>.</li>
  </ul>
  
  <p>I can recursively clean up the files.  How do I clean up the subfolders without deleting <code>subfolder1a</code>?  (The "Junk" folder will always remain.)</p>