Skip to content

$cs.File_DelFile

Deletes a file. By default, only the exact file at filepath is deleted, and nothing happens if it doesn’t exist. If recursive = $true, the filename portion of filepath is instead treated as a search pattern (wildcards allowed, e.g. *.tmp) and every matching file found anywhere under filepath’s parent folder tree is deleted, case-insensitively.

$cs.File_DelFile(string filepath, bool recursive = false)

File to delete. When recursive is $true, the filename part of this path is used as a wildcard search pattern instead.

If $true, searches the parent folder tree of filepath for files matching the filename pattern and deletes all matches. Default is $false (deletes only the exact file). Can throw an error if a matched file cannot be deleted due to permissions.

None.

Terminal window
$cs.File_DelFile("C:\Program Files\CapaInstaller\dummy.exe")

To delete every *.tmp file anywhere under a folder tree:

Terminal window
$cs.File_DelFile("C:\Program Files\CapaInstaller\*.tmp", $true)

In PostInstall, remove a single leftover file the installer left behind:

Terminal window
$cs.File_DelFile((Join-Path $env:TEMP "setup_bootstrapper.exe"))

Clean up every *.log file scattered under an application’s data folder tree during uninstall:

Terminal window
$cs.File_DelFile("C:\ProgramData\Contoso\App\*.log", $true)

$cs.File_DelTree $cs.File_ExistFile