#requires -version 2.0 # Jeffery Hicks # http://jdhitsolutions.com/blog # follow on Twitter: http://twitter.com/JeffHicks # "Those who forget to script are doomed to repeat their work." # **************************************************************** # * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED * # * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF * # * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, * # * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. * # **************************************************************** Function IsEmptyFolder { #requires -version 2.0 <# .Synopsis Determine if a folder is empty of all files. .Description This function can be used to determine if a folder is empty of all files. It will ignore any subfolders, unless that subfolder includes files. If the folder is empty, the fullname is written to the pipeline. You can capture this information to a file or variable or pass it on to Remove-Item to delete the folder. .Parameter Fullname The full folder path to examine .Example PS C:\> IsEmptyFolder "c:\files\test" Will return the folder full name if c:\files\test has no files. .Example ps C:\> dir c:\test -recurse | where {$_.PSIsContainer} | IsEmptyFolder | Sort This is a bit more efficient as only directory paths are passed to the function. Output is sorted by the fullname. .Example PS C:\> dir $env:temp -recurse | IsEmptyFolder | out-file Cleanup.txt Find all empty folders in the %TEMP% directory and save the paths to a text file. .Example PS C:\> dir $env:temp -recurse | IsEmptyFolder | Remove-Item -recurse Find and delete all empty folders under %TEMP%. .Inputs Accepts strings or directoryinfo objects as pipelined input .Outputs [string] .Link Get-ChildItem .Notes NAME: IsEmptyFolder Function VERSION: 1.0 AUTHOR: Jeffery Hicks LASTEDIT: 10/1/2009 #> [CmdletBinding()] Param ( [Parameter(ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True, Position=1, Mandatory=$False, HelpMessage="Enter a folder name and path.")] [string[]]$fullname="." ) Begin { Write-Verbose "Starting function" Write-Debug "Starting function" Write-Debug "Running as $env:userdomain\$env:username" } #end Begin Process { Trap { Write-Warning "Failed to find or enumerate $fullname" Write-Verbose "Exception raised and caught by trap" Write-Debug "Exception raised and caught by trap" Write-Debug $_.Exception Continue } Write-Verbose "Testing $fullname" Write-Debug "Testing $fullname" If ((dir $fullname -force -recurse -ea stop | where {-Not $_.PSIsContainer} | measure-object).Count -eq 0) { Write-Verbose "Writing empty folder fullname" Write-Debug "Empty folder found" if ($_) { Write-Debug "Current object is a $($_.GetType())" } #if a directory object was passed through the pipeline then we can use the object's full name. if ($_ -is [System.IO.DirectoryInfo]) { write $_.Fullname } else { #otherwise write the value passed as a parameter write $fullname } } } #end Process End { Write-Verbose "Ending function" Write-Debug "Ending function" } #end End } #end function #create an alias for the function Set-Alias ief IsEmptyFolderas ief IsEmptyFolderas ief IsEmptyFolderderderderderderderderderderderderderderderderderderder