Find All Empty folder on my File Server

Welcome Forums General PowerShell Q&A Find All Empty folder on my File Server

Viewing 3 reply threads
  • Author
    Posts
    • #261084
      Participant
      Topics: 38
      Replies: 116
      Points: 622
      Rank: Major Contributor

      I have a drive with user folders and I am looking to delete all the folders that are empty on the file system (NTFS)

      I tried this code

      $SomePath=”j:\users”

      Get-ChildItem -Path $SomePath -Recurse -Directory | ForEach-Object -Process {
      if ($false -eq $_.GetFileSystemInfos())
      {
      $_.FullName
      }
      }

      I only need the top level folders that are empty not a sub folder within a sub folder that is empty

      Many folders do not have any folders or files in them some folders have files and folders these I want to exclude. If they have a folder that is empty I do not care at this point about those empty folders Only the top level

      Example

       

      J:\Users
      UserA
      FolderA
      Files
      UserB
      UserC
      UserD
      FolderA      Empty
      FolderB
      Files

      I the above example  I would like only UserB and UserC

      Any ideas

       

      Thank you

      Tom

       

       

       

    • #261123
      Participant
      Topics: 8
      Replies: 617
      Points: 2,393
      Helping Hand
      Rank: Community Hero

      Please read how to format code/errors as it makes it difficult or even undesirable to help you.

      You were close, just use a where clause

      You will end up with an object that has a fullname property. If you want to just get the fullnames, add -ExpandProperty to the select statement.

    • #261215
      Participant
      Topics: 38
      Replies: 116
      Points: 622
      Rank: Major Contributor

      Doug

       

      Thanks for the help

       

      I ran both to see what was best , they both worked with one problem

      It seems it loops thru the folders a second time a list them all again

      Any thoughts or suggestions?

       

      Thanks

      Tom

    • #261248
      Participant
      Topics: 8
      Replies: 617
      Points: 2,393
      Helping Hand
      Rank: Community Hero

      Perhaps you can show the code that you ran to produce those results. Here’s a test you can run to confirm the results.

      This test expects c:\temp\demo to exist already.

      There will be 4 folders created in demo one of them being empty. It also creates 4 subfolders and one of those is empty. The contents are a mix of files/folders to ensure correct behavior. The output correctly lists only the two empty folders.

Viewing 3 reply threads
  • You must be logged in to reply to this topic.