Wednesday 14 November 2018

Remove folder if empty

The following PowerShell code is part of a larger App Deployment Toolkit for uninstalling an application. As programs are usually organised into department-specific folders on the Start Menu, it is necessary to delete them during the uninstall process, but taking into account that other programs may have shortcuts in there too (hence their scripts also have this uninstall snippet). It performs a count of the objects inside the folder to determine if it can be removed.

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
#Get any items within the 'Maths & Stats' folder and count them
If ((Get-ChildItem -Path "$envProgramData\Microsoft\Windows\Start Menu\Programs\Maths & Stats").count -eq 0) {

     
    #If there are zero items then delete the whole folder
    Remove-Folder -Path "$envProgramData\Microsoft\Windows\Start Menu\Programs\Maths & Stats"
}

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

Remove folder if empty.ps1

No comments:

Post a Comment