Skip to content

$cs.File_FindFile

Searches for files matching a name or wildcard pattern starting at searchroot, and returns the full paths of every match. Throws ArgumentNullException if filename is empty, and DirectoryNotFoundException if searchroot doesn’t exist.

$cs.File_FindFile(string filename, string searchroot, bool recursive)

Name or wildcard pattern of the file(s) to search for, e.g. "*.log".

Directory to start the search from. Must exist.

$true to search searchroot and every subfolder beneath it; $false to search only searchroot itself.

A list of strings containing the full path of every matching file. Returns an empty list (not $null) if nothing is found.

Terminal window
$searchRoot = "C:\Users"
if ($cs.File_ExistDir($searchRoot))
{
$updateFiles = $cs.File_FindFile("update.exe", $searchRoot, $true)
$cs.Job_WriteLog("Found $($updateFiles.Count) matching file(s)")
}

Recursively search a data folder tree for a specific log file left behind by the vendor installer:

Terminal window
$dataRoot = Join-Path $env:ProgramData "Contoso"
if ($cs.File_ExistDir($dataRoot))
{
$matches = $cs.File_FindFile("setup.log", $dataRoot, $true)
foreach ($match in $matches)
{
$cs.Job_WriteLog("Found setup log at $match")
}
}

Restrict the search to just the kit’s top folder (non-recursive) to locate the bundled MSI without descending into subfolders:

Terminal window
$kitRoot = Join-Path $global:Packageroot "kit"
$msiFiles = $cs.File_FindFile("*.msi", $kitRoot, $false)
if ($msiFiles.Count -eq 0)
{
Exit-PSScript -exitcode 1 -exitmessage "No MSI found directly under $kitRoot, aborting"
}

$cs.File_ExistFile $cs.File_ExistDir