Skip to content

$cs.File_ExistDir

Checks whether a directory exists. Never throws — it simply returns $false if the directory is missing.

$cs.File_ExistDir(string path)

Full path of the directory to check.

Boolean, $true if the directory exists, $false otherwise.

Terminal window
$installFolder = Join-Path ${env:ProgramFiles(x86)} "WinSCP"
if ($cs.File_ExistDir($installFolder))
{
$cs.Job_WriteLog("WinSCP folder found, skipping creation")
}
else
{
$cs.File_CreateDirectory($installFolder)
}

In Uninstall, skip a cleanup step entirely if the data folder was never created:

Terminal window
$dataFolder = Join-Path $env:ProgramData "Contoso\AppData"
if ($cs.File_ExistDir($dataFolder))
{
$cs.Job_WriteLog("Removing leftover data folder $dataFolder")
$cs.File_DelTree($dataFolder)
}
else
{
$cs.Job_WriteLog("No data folder present, nothing to clean up")
}

A PreInstall check that a required source share is reachable before continuing:

Terminal window
$sourceShare = "\\fileserver01\Deploy\Contoso"
if (!$cs.File_ExistDir($sourceShare))
{
Exit-PSScript -exitcode 1 -exitmessage "Source share $sourceShare is not reachable, aborting install"
}

$cs.File_ExistFile $cs.File_CreateDirectory $cs.File_DelTree