Skip to content

$cs.File_DelEmptyFolder

Deletes the folder at path, but only if it is completely empty. “Empty” is determined by summing the size of every file anywhere under the folder, recursively — so a folder that only contains empty subfolders is still deleted. If path doesn’t exist, or if it isn’t empty, the function does nothing and does not raise an error.

$cs.File_DelEmptyFolder(string path)

Folder to delete if it is empty.

None.

Terminal window
$cs.File_DelEmptyFolder("$env:ProgramData\Notepad++")

In PostUninstall, after removing the application’s files, use it to tidy up the install folder only if nothing else was left behind in it:

Terminal window
$InstallFolder = "C:\Program Files\Contoso\App"
$cs.File_DelFile((Join-Path $InstallFolder "app.exe"))
$cs.File_DelEmptyFolder($InstallFolder)

It’s also safe to call on a folder that only contains empty subfolders, since emptiness is checked recursively by total file size:

Terminal window
$cs.File_DelEmptyFolder("$env:ProgramData\Contoso\App\Cache")

$cs.File_DelTree $cs.File_ExistDir