How to find empty directories in Windows using a Powershell Script












8















What command do you use to find empty directories in Windows?



Some folders might contain some hidden folders like .svn or .settings, but they should still be treated as empty folders.










share|improve this question

























  • What version of Windows are you running?

    – Windos
    Aug 10 '11 at 5:41
















8















What command do you use to find empty directories in Windows?



Some folders might contain some hidden folders like .svn or .settings, but they should still be treated as empty folders.










share|improve this question

























  • What version of Windows are you running?

    – Windos
    Aug 10 '11 at 5:41














8












8








8


2






What command do you use to find empty directories in Windows?



Some folders might contain some hidden folders like .svn or .settings, but they should still be treated as empty folders.










share|improve this question
















What command do you use to find empty directories in Windows?



Some folders might contain some hidden folders like .svn or .settings, but they should still be treated as empty folders.







windows powershell






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 10 '11 at 16:45









Moab

51.2k1494160




51.2k1494160










asked Aug 10 '11 at 5:32









JoshuaJoshua

3212414




3212414













  • What version of Windows are you running?

    – Windos
    Aug 10 '11 at 5:41



















  • What version of Windows are you running?

    – Windos
    Aug 10 '11 at 5:41

















What version of Windows are you running?

– Windos
Aug 10 '11 at 5:41





What version of Windows are you running?

– Windos
Aug 10 '11 at 5:41










4 Answers
4






active

oldest

votes


















12














Easiest way I can think of is with a small PowerShell script. If you're running Windows 7 you should have it installed already, if not visit Microsoft.com to download and install it. The link provides a detailed description but the jist of the operation is included here for you convenience.



Open PowerShell and enter this:



(gci C:Scripts -r | ? {$_.PSIsContainer -eq $True}) | ? {$_.GetFiles().Count -eq 0} | select FullName


Change C:Scripts to whatever you want to search through, you can even set it to just C: if you want it to check the entire drive.



It will give you output like this (note these are the empty directories below C:Scripts.



FullName
-------
C:ScriptsEmpty
C:ScriptsEmpty Folder 2
C:ScriptsEmptyEmpty Subfolder
C:ScriptsNew FolderEmpty Subfolder Three Levels Deep


If you look into PowerShell a bit I'm sure you'll be able to figure out how to automatically delete empty folders if you want to (though I recommend against it, just in case.)



Edit: As Richard mentioned in the comments, for a truly empty directory use:



(gci C:Scripts -r | ? {$_.PSIsContainer -eq $True}) | ?{$_.GetFileSystemInfos().Count -eq 0} | select FullName





share|improve this answer


























  • am using Windows Vista

    – Joshua
    Aug 10 '11 at 5:56











  • PowerShell for Vista can be downloaded here: microsoft.com/download/en/details.aspx?id=23200

    – Windos
    Aug 10 '11 at 5:58






  • 7





    Note: that pipeline will find folders that contain no files but do contain other folders. To have completely empty folders change third element of the pipe to ?{$_.GetFileSystemInfos().Count -eq 0}.

    – Richard
    Aug 10 '11 at 6:14








  • 1





    @Joshua, you didn't change "C:Scripts" to where you want to search, hence you get the error "Cannot find path 'C:Scripts' because it does not exist." It's right at the start of the line.

    – Windos
    Aug 10 '11 at 9:35








  • 1





    @Joshua, have you read Richards comment (third one in this thread) and my edit to the answer? That should only give you directories with no files and no child directories (i.e. the leaf nodes)

    – Windos
    Aug 10 '11 at 10:40



















2














The following is the easiest way I could find to achieve this with a single line of code. It lists the empty directories at the current location. If recursion is needed the parameter-Recurse could be added to the call to Get-ChildItem.



Get-ChildItem -Directory | Where-Object { $_.GetFileSystemInfos().Count -eq 0 }



Short version with aliases:



dir -Directory | ? {$_.GetFileSystemInfos().Count -eq 0 }



Or, as a parameterized PowerShell function (I added this to my PowerShell startup profile):



Function Get-EmptyDirectories($basedir) { 
Get-ChildItem -Directory $basedir | Where-Object { $_.GetFileSystemInfos().Count -eq 0 }
}


This can then be invoked as any other PowerShell function, including piping. For example, this call would delete all empty directories in the system temp directory:



Get-EmptyDirectories $env:TMP | del






share|improve this answer































    1














    Thanks, I used this as a basis for my script. I wanted to delete empty folders but trying to do Where-Object {$_.GetFiles().Count -eq 0} would delete folders that had sub-directories that were not empty. I ended up using a DO WHILE loop to remove a folder that had no files or folders then loop back and check again until it reached the end of the tree.



    $Datefn=Get-Date -format M.d.yyyy_HH.mm.ss
    #Set The File Name for the log file
    $DelFileName = $Datefn
    #Set The File Ext for the log file
    $DelFileExt = " - Old Files" + ".log"
    #Set The File Name With Ext for the log file
    $DelFileName = $DelFileName + $DelFileExt
    #Set Log Path
    $LogPath = [Environment]::GetFolderPath("Desktop")
    $Path = 'Q:'
    $NumDays = 365
    Get-ChildItem -Path $Path -Exclude DCID.txt,*.exe -Recurse | Where-Object {$_.lastwritetime -lt`
    (Get-Date).addDays(-$NumDays) -and $_.psiscontainer -eq $false} |
    ForEach-Object {
    $properties = @{`
    Path = $_.Directory`
    Name = $_.Name
    DateModified = $_.LastWriteTime
    Size = $_.Length / 1GB }
    New-Object PSObject -Property $properties | select Path,Name,DateModified, Size
    } |
    Out-File "$LogPath$DelFileName"
    <#
    #Removes the files found
    Get-ChildItem -Path $Path -Exclude DCID.txt,*.exe -Recurse | Where-Object {$_.lastwritetime -lt`
    (Get-Date).addDays(-365) -and $_.psiscontainer -eq $false} | Remove-Item -Recurse -Force
    #Removes empty folders
    DO {
    $a = (Get-ChildItem $Path -Recurse | Where-Object {$_.PSIsContainer -eq $true}) | Where-Object`
    {$_.GetFileSystemInfos().Count -eq 0} | Select-Object Fullname
    $a
    (Get-ChildItem $Path -Recurse | Where-Object {$_.PSIsContainer -eq $true}) | Where-Object`
    {$_.GetFileSystemInfos().Count -eq 0} | Remove-Item -Force
    }
    WHILE ($a -ne $null)
    #>





    share|improve this answer

































      1














      Try this



      Get-ChildItem C:Scripts -Recurse -Directory | Where-Object {!$_.GetFileSystemInfos().Count}


      The count is not 0, it doesn't exist at all meaning that the directory is completely empty or holds other completely empty folders






      share|improve this answer























        Your Answer








        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "3"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f321231%2fhow-to-find-empty-directories-in-windows-using-a-powershell-script%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        12














        Easiest way I can think of is with a small PowerShell script. If you're running Windows 7 you should have it installed already, if not visit Microsoft.com to download and install it. The link provides a detailed description but the jist of the operation is included here for you convenience.



        Open PowerShell and enter this:



        (gci C:Scripts -r | ? {$_.PSIsContainer -eq $True}) | ? {$_.GetFiles().Count -eq 0} | select FullName


        Change C:Scripts to whatever you want to search through, you can even set it to just C: if you want it to check the entire drive.



        It will give you output like this (note these are the empty directories below C:Scripts.



        FullName
        -------
        C:ScriptsEmpty
        C:ScriptsEmpty Folder 2
        C:ScriptsEmptyEmpty Subfolder
        C:ScriptsNew FolderEmpty Subfolder Three Levels Deep


        If you look into PowerShell a bit I'm sure you'll be able to figure out how to automatically delete empty folders if you want to (though I recommend against it, just in case.)



        Edit: As Richard mentioned in the comments, for a truly empty directory use:



        (gci C:Scripts -r | ? {$_.PSIsContainer -eq $True}) | ?{$_.GetFileSystemInfos().Count -eq 0} | select FullName





        share|improve this answer


























        • am using Windows Vista

          – Joshua
          Aug 10 '11 at 5:56











        • PowerShell for Vista can be downloaded here: microsoft.com/download/en/details.aspx?id=23200

          – Windos
          Aug 10 '11 at 5:58






        • 7





          Note: that pipeline will find folders that contain no files but do contain other folders. To have completely empty folders change third element of the pipe to ?{$_.GetFileSystemInfos().Count -eq 0}.

          – Richard
          Aug 10 '11 at 6:14








        • 1





          @Joshua, you didn't change "C:Scripts" to where you want to search, hence you get the error "Cannot find path 'C:Scripts' because it does not exist." It's right at the start of the line.

          – Windos
          Aug 10 '11 at 9:35








        • 1





          @Joshua, have you read Richards comment (third one in this thread) and my edit to the answer? That should only give you directories with no files and no child directories (i.e. the leaf nodes)

          – Windos
          Aug 10 '11 at 10:40
















        12














        Easiest way I can think of is with a small PowerShell script. If you're running Windows 7 you should have it installed already, if not visit Microsoft.com to download and install it. The link provides a detailed description but the jist of the operation is included here for you convenience.



        Open PowerShell and enter this:



        (gci C:Scripts -r | ? {$_.PSIsContainer -eq $True}) | ? {$_.GetFiles().Count -eq 0} | select FullName


        Change C:Scripts to whatever you want to search through, you can even set it to just C: if you want it to check the entire drive.



        It will give you output like this (note these are the empty directories below C:Scripts.



        FullName
        -------
        C:ScriptsEmpty
        C:ScriptsEmpty Folder 2
        C:ScriptsEmptyEmpty Subfolder
        C:ScriptsNew FolderEmpty Subfolder Three Levels Deep


        If you look into PowerShell a bit I'm sure you'll be able to figure out how to automatically delete empty folders if you want to (though I recommend against it, just in case.)



        Edit: As Richard mentioned in the comments, for a truly empty directory use:



        (gci C:Scripts -r | ? {$_.PSIsContainer -eq $True}) | ?{$_.GetFileSystemInfos().Count -eq 0} | select FullName





        share|improve this answer


























        • am using Windows Vista

          – Joshua
          Aug 10 '11 at 5:56











        • PowerShell for Vista can be downloaded here: microsoft.com/download/en/details.aspx?id=23200

          – Windos
          Aug 10 '11 at 5:58






        • 7





          Note: that pipeline will find folders that contain no files but do contain other folders. To have completely empty folders change third element of the pipe to ?{$_.GetFileSystemInfos().Count -eq 0}.

          – Richard
          Aug 10 '11 at 6:14








        • 1





          @Joshua, you didn't change "C:Scripts" to where you want to search, hence you get the error "Cannot find path 'C:Scripts' because it does not exist." It's right at the start of the line.

          – Windos
          Aug 10 '11 at 9:35








        • 1





          @Joshua, have you read Richards comment (third one in this thread) and my edit to the answer? That should only give you directories with no files and no child directories (i.e. the leaf nodes)

          – Windos
          Aug 10 '11 at 10:40














        12












        12








        12







        Easiest way I can think of is with a small PowerShell script. If you're running Windows 7 you should have it installed already, if not visit Microsoft.com to download and install it. The link provides a detailed description but the jist of the operation is included here for you convenience.



        Open PowerShell and enter this:



        (gci C:Scripts -r | ? {$_.PSIsContainer -eq $True}) | ? {$_.GetFiles().Count -eq 0} | select FullName


        Change C:Scripts to whatever you want to search through, you can even set it to just C: if you want it to check the entire drive.



        It will give you output like this (note these are the empty directories below C:Scripts.



        FullName
        -------
        C:ScriptsEmpty
        C:ScriptsEmpty Folder 2
        C:ScriptsEmptyEmpty Subfolder
        C:ScriptsNew FolderEmpty Subfolder Three Levels Deep


        If you look into PowerShell a bit I'm sure you'll be able to figure out how to automatically delete empty folders if you want to (though I recommend against it, just in case.)



        Edit: As Richard mentioned in the comments, for a truly empty directory use:



        (gci C:Scripts -r | ? {$_.PSIsContainer -eq $True}) | ?{$_.GetFileSystemInfos().Count -eq 0} | select FullName





        share|improve this answer















        Easiest way I can think of is with a small PowerShell script. If you're running Windows 7 you should have it installed already, if not visit Microsoft.com to download and install it. The link provides a detailed description but the jist of the operation is included here for you convenience.



        Open PowerShell and enter this:



        (gci C:Scripts -r | ? {$_.PSIsContainer -eq $True}) | ? {$_.GetFiles().Count -eq 0} | select FullName


        Change C:Scripts to whatever you want to search through, you can even set it to just C: if you want it to check the entire drive.



        It will give you output like this (note these are the empty directories below C:Scripts.



        FullName
        -------
        C:ScriptsEmpty
        C:ScriptsEmpty Folder 2
        C:ScriptsEmptyEmpty Subfolder
        C:ScriptsNew FolderEmpty Subfolder Three Levels Deep


        If you look into PowerShell a bit I'm sure you'll be able to figure out how to automatically delete empty folders if you want to (though I recommend against it, just in case.)



        Edit: As Richard mentioned in the comments, for a truly empty directory use:



        (gci C:Scripts -r | ? {$_.PSIsContainer -eq $True}) | ?{$_.GetFileSystemInfos().Count -eq 0} | select FullName






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Aug 10 '11 at 6:25

























        answered Aug 10 '11 at 5:49









        WindosWindos

        9,70132955




        9,70132955













        • am using Windows Vista

          – Joshua
          Aug 10 '11 at 5:56











        • PowerShell for Vista can be downloaded here: microsoft.com/download/en/details.aspx?id=23200

          – Windos
          Aug 10 '11 at 5:58






        • 7





          Note: that pipeline will find folders that contain no files but do contain other folders. To have completely empty folders change third element of the pipe to ?{$_.GetFileSystemInfos().Count -eq 0}.

          – Richard
          Aug 10 '11 at 6:14








        • 1





          @Joshua, you didn't change "C:Scripts" to where you want to search, hence you get the error "Cannot find path 'C:Scripts' because it does not exist." It's right at the start of the line.

          – Windos
          Aug 10 '11 at 9:35








        • 1





          @Joshua, have you read Richards comment (third one in this thread) and my edit to the answer? That should only give you directories with no files and no child directories (i.e. the leaf nodes)

          – Windos
          Aug 10 '11 at 10:40



















        • am using Windows Vista

          – Joshua
          Aug 10 '11 at 5:56











        • PowerShell for Vista can be downloaded here: microsoft.com/download/en/details.aspx?id=23200

          – Windos
          Aug 10 '11 at 5:58






        • 7





          Note: that pipeline will find folders that contain no files but do contain other folders. To have completely empty folders change third element of the pipe to ?{$_.GetFileSystemInfos().Count -eq 0}.

          – Richard
          Aug 10 '11 at 6:14








        • 1





          @Joshua, you didn't change "C:Scripts" to where you want to search, hence you get the error "Cannot find path 'C:Scripts' because it does not exist." It's right at the start of the line.

          – Windos
          Aug 10 '11 at 9:35








        • 1





          @Joshua, have you read Richards comment (third one in this thread) and my edit to the answer? That should only give you directories with no files and no child directories (i.e. the leaf nodes)

          – Windos
          Aug 10 '11 at 10:40

















        am using Windows Vista

        – Joshua
        Aug 10 '11 at 5:56





        am using Windows Vista

        – Joshua
        Aug 10 '11 at 5:56













        PowerShell for Vista can be downloaded here: microsoft.com/download/en/details.aspx?id=23200

        – Windos
        Aug 10 '11 at 5:58





        PowerShell for Vista can be downloaded here: microsoft.com/download/en/details.aspx?id=23200

        – Windos
        Aug 10 '11 at 5:58




        7




        7





        Note: that pipeline will find folders that contain no files but do contain other folders. To have completely empty folders change third element of the pipe to ?{$_.GetFileSystemInfos().Count -eq 0}.

        – Richard
        Aug 10 '11 at 6:14







        Note: that pipeline will find folders that contain no files but do contain other folders. To have completely empty folders change third element of the pipe to ?{$_.GetFileSystemInfos().Count -eq 0}.

        – Richard
        Aug 10 '11 at 6:14






        1




        1





        @Joshua, you didn't change "C:Scripts" to where you want to search, hence you get the error "Cannot find path 'C:Scripts' because it does not exist." It's right at the start of the line.

        – Windos
        Aug 10 '11 at 9:35







        @Joshua, you didn't change "C:Scripts" to where you want to search, hence you get the error "Cannot find path 'C:Scripts' because it does not exist." It's right at the start of the line.

        – Windos
        Aug 10 '11 at 9:35






        1




        1





        @Joshua, have you read Richards comment (third one in this thread) and my edit to the answer? That should only give you directories with no files and no child directories (i.e. the leaf nodes)

        – Windos
        Aug 10 '11 at 10:40





        @Joshua, have you read Richards comment (third one in this thread) and my edit to the answer? That should only give you directories with no files and no child directories (i.e. the leaf nodes)

        – Windos
        Aug 10 '11 at 10:40













        2














        The following is the easiest way I could find to achieve this with a single line of code. It lists the empty directories at the current location. If recursion is needed the parameter-Recurse could be added to the call to Get-ChildItem.



        Get-ChildItem -Directory | Where-Object { $_.GetFileSystemInfos().Count -eq 0 }



        Short version with aliases:



        dir -Directory | ? {$_.GetFileSystemInfos().Count -eq 0 }



        Or, as a parameterized PowerShell function (I added this to my PowerShell startup profile):



        Function Get-EmptyDirectories($basedir) { 
        Get-ChildItem -Directory $basedir | Where-Object { $_.GetFileSystemInfos().Count -eq 0 }
        }


        This can then be invoked as any other PowerShell function, including piping. For example, this call would delete all empty directories in the system temp directory:



        Get-EmptyDirectories $env:TMP | del






        share|improve this answer




























          2














          The following is the easiest way I could find to achieve this with a single line of code. It lists the empty directories at the current location. If recursion is needed the parameter-Recurse could be added to the call to Get-ChildItem.



          Get-ChildItem -Directory | Where-Object { $_.GetFileSystemInfos().Count -eq 0 }



          Short version with aliases:



          dir -Directory | ? {$_.GetFileSystemInfos().Count -eq 0 }



          Or, as a parameterized PowerShell function (I added this to my PowerShell startup profile):



          Function Get-EmptyDirectories($basedir) { 
          Get-ChildItem -Directory $basedir | Where-Object { $_.GetFileSystemInfos().Count -eq 0 }
          }


          This can then be invoked as any other PowerShell function, including piping. For example, this call would delete all empty directories in the system temp directory:



          Get-EmptyDirectories $env:TMP | del






          share|improve this answer


























            2












            2








            2







            The following is the easiest way I could find to achieve this with a single line of code. It lists the empty directories at the current location. If recursion is needed the parameter-Recurse could be added to the call to Get-ChildItem.



            Get-ChildItem -Directory | Where-Object { $_.GetFileSystemInfos().Count -eq 0 }



            Short version with aliases:



            dir -Directory | ? {$_.GetFileSystemInfos().Count -eq 0 }



            Or, as a parameterized PowerShell function (I added this to my PowerShell startup profile):



            Function Get-EmptyDirectories($basedir) { 
            Get-ChildItem -Directory $basedir | Where-Object { $_.GetFileSystemInfos().Count -eq 0 }
            }


            This can then be invoked as any other PowerShell function, including piping. For example, this call would delete all empty directories in the system temp directory:



            Get-EmptyDirectories $env:TMP | del






            share|improve this answer













            The following is the easiest way I could find to achieve this with a single line of code. It lists the empty directories at the current location. If recursion is needed the parameter-Recurse could be added to the call to Get-ChildItem.



            Get-ChildItem -Directory | Where-Object { $_.GetFileSystemInfos().Count -eq 0 }



            Short version with aliases:



            dir -Directory | ? {$_.GetFileSystemInfos().Count -eq 0 }



            Or, as a parameterized PowerShell function (I added this to my PowerShell startup profile):



            Function Get-EmptyDirectories($basedir) { 
            Get-ChildItem -Directory $basedir | Where-Object { $_.GetFileSystemInfos().Count -eq 0 }
            }


            This can then be invoked as any other PowerShell function, including piping. For example, this call would delete all empty directories in the system temp directory:



            Get-EmptyDirectories $env:TMP | del







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 12 '18 at 11:00









            esoeso

            212




            212























                1














                Thanks, I used this as a basis for my script. I wanted to delete empty folders but trying to do Where-Object {$_.GetFiles().Count -eq 0} would delete folders that had sub-directories that were not empty. I ended up using a DO WHILE loop to remove a folder that had no files or folders then loop back and check again until it reached the end of the tree.



                $Datefn=Get-Date -format M.d.yyyy_HH.mm.ss
                #Set The File Name for the log file
                $DelFileName = $Datefn
                #Set The File Ext for the log file
                $DelFileExt = " - Old Files" + ".log"
                #Set The File Name With Ext for the log file
                $DelFileName = $DelFileName + $DelFileExt
                #Set Log Path
                $LogPath = [Environment]::GetFolderPath("Desktop")
                $Path = 'Q:'
                $NumDays = 365
                Get-ChildItem -Path $Path -Exclude DCID.txt,*.exe -Recurse | Where-Object {$_.lastwritetime -lt`
                (Get-Date).addDays(-$NumDays) -and $_.psiscontainer -eq $false} |
                ForEach-Object {
                $properties = @{`
                Path = $_.Directory`
                Name = $_.Name
                DateModified = $_.LastWriteTime
                Size = $_.Length / 1GB }
                New-Object PSObject -Property $properties | select Path,Name,DateModified, Size
                } |
                Out-File "$LogPath$DelFileName"
                <#
                #Removes the files found
                Get-ChildItem -Path $Path -Exclude DCID.txt,*.exe -Recurse | Where-Object {$_.lastwritetime -lt`
                (Get-Date).addDays(-365) -and $_.psiscontainer -eq $false} | Remove-Item -Recurse -Force
                #Removes empty folders
                DO {
                $a = (Get-ChildItem $Path -Recurse | Where-Object {$_.PSIsContainer -eq $true}) | Where-Object`
                {$_.GetFileSystemInfos().Count -eq 0} | Select-Object Fullname
                $a
                (Get-ChildItem $Path -Recurse | Where-Object {$_.PSIsContainer -eq $true}) | Where-Object`
                {$_.GetFileSystemInfos().Count -eq 0} | Remove-Item -Force
                }
                WHILE ($a -ne $null)
                #>





                share|improve this answer






























                  1














                  Thanks, I used this as a basis for my script. I wanted to delete empty folders but trying to do Where-Object {$_.GetFiles().Count -eq 0} would delete folders that had sub-directories that were not empty. I ended up using a DO WHILE loop to remove a folder that had no files or folders then loop back and check again until it reached the end of the tree.



                  $Datefn=Get-Date -format M.d.yyyy_HH.mm.ss
                  #Set The File Name for the log file
                  $DelFileName = $Datefn
                  #Set The File Ext for the log file
                  $DelFileExt = " - Old Files" + ".log"
                  #Set The File Name With Ext for the log file
                  $DelFileName = $DelFileName + $DelFileExt
                  #Set Log Path
                  $LogPath = [Environment]::GetFolderPath("Desktop")
                  $Path = 'Q:'
                  $NumDays = 365
                  Get-ChildItem -Path $Path -Exclude DCID.txt,*.exe -Recurse | Where-Object {$_.lastwritetime -lt`
                  (Get-Date).addDays(-$NumDays) -and $_.psiscontainer -eq $false} |
                  ForEach-Object {
                  $properties = @{`
                  Path = $_.Directory`
                  Name = $_.Name
                  DateModified = $_.LastWriteTime
                  Size = $_.Length / 1GB }
                  New-Object PSObject -Property $properties | select Path,Name,DateModified, Size
                  } |
                  Out-File "$LogPath$DelFileName"
                  <#
                  #Removes the files found
                  Get-ChildItem -Path $Path -Exclude DCID.txt,*.exe -Recurse | Where-Object {$_.lastwritetime -lt`
                  (Get-Date).addDays(-365) -and $_.psiscontainer -eq $false} | Remove-Item -Recurse -Force
                  #Removes empty folders
                  DO {
                  $a = (Get-ChildItem $Path -Recurse | Where-Object {$_.PSIsContainer -eq $true}) | Where-Object`
                  {$_.GetFileSystemInfos().Count -eq 0} | Select-Object Fullname
                  $a
                  (Get-ChildItem $Path -Recurse | Where-Object {$_.PSIsContainer -eq $true}) | Where-Object`
                  {$_.GetFileSystemInfos().Count -eq 0} | Remove-Item -Force
                  }
                  WHILE ($a -ne $null)
                  #>





                  share|improve this answer




























                    1












                    1








                    1







                    Thanks, I used this as a basis for my script. I wanted to delete empty folders but trying to do Where-Object {$_.GetFiles().Count -eq 0} would delete folders that had sub-directories that were not empty. I ended up using a DO WHILE loop to remove a folder that had no files or folders then loop back and check again until it reached the end of the tree.



                    $Datefn=Get-Date -format M.d.yyyy_HH.mm.ss
                    #Set The File Name for the log file
                    $DelFileName = $Datefn
                    #Set The File Ext for the log file
                    $DelFileExt = " - Old Files" + ".log"
                    #Set The File Name With Ext for the log file
                    $DelFileName = $DelFileName + $DelFileExt
                    #Set Log Path
                    $LogPath = [Environment]::GetFolderPath("Desktop")
                    $Path = 'Q:'
                    $NumDays = 365
                    Get-ChildItem -Path $Path -Exclude DCID.txt,*.exe -Recurse | Where-Object {$_.lastwritetime -lt`
                    (Get-Date).addDays(-$NumDays) -and $_.psiscontainer -eq $false} |
                    ForEach-Object {
                    $properties = @{`
                    Path = $_.Directory`
                    Name = $_.Name
                    DateModified = $_.LastWriteTime
                    Size = $_.Length / 1GB }
                    New-Object PSObject -Property $properties | select Path,Name,DateModified, Size
                    } |
                    Out-File "$LogPath$DelFileName"
                    <#
                    #Removes the files found
                    Get-ChildItem -Path $Path -Exclude DCID.txt,*.exe -Recurse | Where-Object {$_.lastwritetime -lt`
                    (Get-Date).addDays(-365) -and $_.psiscontainer -eq $false} | Remove-Item -Recurse -Force
                    #Removes empty folders
                    DO {
                    $a = (Get-ChildItem $Path -Recurse | Where-Object {$_.PSIsContainer -eq $true}) | Where-Object`
                    {$_.GetFileSystemInfos().Count -eq 0} | Select-Object Fullname
                    $a
                    (Get-ChildItem $Path -Recurse | Where-Object {$_.PSIsContainer -eq $true}) | Where-Object`
                    {$_.GetFileSystemInfos().Count -eq 0} | Remove-Item -Force
                    }
                    WHILE ($a -ne $null)
                    #>





                    share|improve this answer















                    Thanks, I used this as a basis for my script. I wanted to delete empty folders but trying to do Where-Object {$_.GetFiles().Count -eq 0} would delete folders that had sub-directories that were not empty. I ended up using a DO WHILE loop to remove a folder that had no files or folders then loop back and check again until it reached the end of the tree.



                    $Datefn=Get-Date -format M.d.yyyy_HH.mm.ss
                    #Set The File Name for the log file
                    $DelFileName = $Datefn
                    #Set The File Ext for the log file
                    $DelFileExt = " - Old Files" + ".log"
                    #Set The File Name With Ext for the log file
                    $DelFileName = $DelFileName + $DelFileExt
                    #Set Log Path
                    $LogPath = [Environment]::GetFolderPath("Desktop")
                    $Path = 'Q:'
                    $NumDays = 365
                    Get-ChildItem -Path $Path -Exclude DCID.txt,*.exe -Recurse | Where-Object {$_.lastwritetime -lt`
                    (Get-Date).addDays(-$NumDays) -and $_.psiscontainer -eq $false} |
                    ForEach-Object {
                    $properties = @{`
                    Path = $_.Directory`
                    Name = $_.Name
                    DateModified = $_.LastWriteTime
                    Size = $_.Length / 1GB }
                    New-Object PSObject -Property $properties | select Path,Name,DateModified, Size
                    } |
                    Out-File "$LogPath$DelFileName"
                    <#
                    #Removes the files found
                    Get-ChildItem -Path $Path -Exclude DCID.txt,*.exe -Recurse | Where-Object {$_.lastwritetime -lt`
                    (Get-Date).addDays(-365) -and $_.psiscontainer -eq $false} | Remove-Item -Recurse -Force
                    #Removes empty folders
                    DO {
                    $a = (Get-ChildItem $Path -Recurse | Where-Object {$_.PSIsContainer -eq $true}) | Where-Object`
                    {$_.GetFileSystemInfos().Count -eq 0} | Select-Object Fullname
                    $a
                    (Get-ChildItem $Path -Recurse | Where-Object {$_.PSIsContainer -eq $true}) | Where-Object`
                    {$_.GetFileSystemInfos().Count -eq 0} | Remove-Item -Force
                    }
                    WHILE ($a -ne $null)
                    #>






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited May 9 '13 at 23:28









                    Smith John

                    206210




                    206210










                    answered May 9 '13 at 23:10









                    MichaelMichael

                    463




                    463























                        1














                        Try this



                        Get-ChildItem C:Scripts -Recurse -Directory | Where-Object {!$_.GetFileSystemInfos().Count}


                        The count is not 0, it doesn't exist at all meaning that the directory is completely empty or holds other completely empty folders






                        share|improve this answer




























                          1














                          Try this



                          Get-ChildItem C:Scripts -Recurse -Directory | Where-Object {!$_.GetFileSystemInfos().Count}


                          The count is not 0, it doesn't exist at all meaning that the directory is completely empty or holds other completely empty folders






                          share|improve this answer


























                            1












                            1








                            1







                            Try this



                            Get-ChildItem C:Scripts -Recurse -Directory | Where-Object {!$_.GetFileSystemInfos().Count}


                            The count is not 0, it doesn't exist at all meaning that the directory is completely empty or holds other completely empty folders






                            share|improve this answer













                            Try this



                            Get-ChildItem C:Scripts -Recurse -Directory | Where-Object {!$_.GetFileSystemInfos().Count}


                            The count is not 0, it doesn't exist at all meaning that the directory is completely empty or holds other completely empty folders







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jan 23 at 15:01









                            Tomer SalakoffTomer Salakoff

                            111




                            111






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Super User!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f321231%2fhow-to-find-empty-directories-in-windows-using-a-powershell-script%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

                                Mangá

                                Eduardo VII do Reino Unido