#PowerShell, #PowerWiseScripting, #ProjectWise, PWPS_DAB

HowTo: Remove Empty Folders Including Current Folder

In this post, we will be demonstrating how you can remove all empty folders from a specified folder. And then remove the current or specified folder if it does not contain and sub-folders or documents.

All of the ProjectWise related cmdlets are available using the PWPS_DAB module. At the time of this post, I am using version 23.2.0.0. Take a look at the help for each of the cmdlets to become familiar with their functionality, available parameters, etc.

  • Show-PWFolderBrowserDialog
  • Remove-PWEmptyFolders
  • Remove-PWFolder

POWERSHELL CODE SNIPPET

The following is a simple PowerShell code snippet to select a ProjectWise folder object, remove all empty folders contained in the selected folder, and then remove the selected folder if it does not contain any sub-folders or documents.

# Select folder to remove empty folders from and delete if contains no sub-folders or documents.
$pwFolder = Show-PWFolderBrowserDialog

# Determine if selected folder has any sub-folders.
if(($pwfolder.GetSubFolders()).Count -gt 0){
    # Remove all empty folders from selected folder.
    Remove-PWEmptyFolders -FolderPath $pwfolder.FullPath 

    <# Determine if the selected folder still has any sub-folders or documents.
        If yes, write warning. If no, remove the selected folder. #>
    if(($pwFolder.GetFolderDocuments()).Count -eq 0 -and ($pwFolder.GetSubFolders()).Count -eq 0){
        Remove-PWFolder -FolderPath $pwfolder.FullPath -ProceedWithDelete -RemoveFolders
    } else {
        Write-Warning -Message "$($pwFolder)"
    }
} # end if(($pwfolder.GetSubFolders()).Count -gt 0...

Experiment with it and have fun. Hopefully, you find this useful.

Please let me know if you have any questions or comments.  If you like this post, please click the Like button at the bottom of the page.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.