Test-Path: Check Files and Paths Exist in CI
Test-Path returns $true if a path exists; with -PathType Leaf or Container it checks specifically for a file or a directory, the standard guard before a CI step touches an artifact.
Before publishing an artifact, restoring a cache, or running a tool, you verify the path exists. Test-Path is the clean boolean check that prevents a confusing downstream error.
What it does
Test-Path evaluates whether the given path exists and returns a boolean. -PathType Leaf requires a file, -PathType Container requires a directory, and -LiteralPath disables wildcard interpretation so paths with [ or ] are matched literally.
Common usage
# guard a publish step on the artifact existing
if (-not (Test-Path .\dist\app.exe -PathType Leaf)) {
throw "Build output missing: dist\app.exe"
}
# create an output dir only if absent
if (-not (Test-Path .\out -PathType Container)) {
New-Item -ItemType Directory .\out | Out-Null
}Options
| Parameter | What it does |
|---|---|
| -Path <p> | Path to test (supports wildcards) |
| -PathType Leaf | True only if it is a file |
| -PathType Container | True only if it is a directory |
| -LiteralPath <p> | No wildcard interpretation (literal path) |
| -IsValid | Check syntax validity, not existence |
In CI
Use -PathType Leaf/Container so a check is precise: a bare Test-Path is true for both a file and a folder of the same name. Combine with throw to fail a step early with a clear message instead of letting a later tool emit a cryptic "file not found".
Common errors in CI
Test-Path returns $true when you expected a file but a *directory* of that name exists; add -PathType Leaf. A path containing [...] returns the wrong result because the brackets are treated as a wildcard range; use -LiteralPath. A relative path testing false usually means the working directory is not what you assumed; print $PWD to confirm.