Skip to content
Latchkey

PowerShell "Test-Path returns false" (case / path separator) in CI

Test-Path reported a path missing even though the file is there. The path string is usually built with the wrong separator, an unescaped wildcard, or a forward-slash assumption that does not resolve.

What this error means

A guard like if (Test-Path $p) takes the wrong branch, then a later step fails because the file was treated as missing (or present). Deterministic for the constructed path.

powershell
# file exists at .\dist\app.js, yet:
PS> Test-Path "dist[]/app.js"
False
# the unescaped [ ] is treated as a wildcard character class

Common causes

Wildcard characters in the path

Test-Path treats [, ], *, and ? as wildcards by default. A literal bracket in a path makes it not match unless you use -LiteralPath.

Path built by naive string concatenation

Mixing separators or doubling them when joining strings produces a path Windows does not resolve.

How to fix it

Use -LiteralPath for paths with special characters

Disable wildcard interpretation so brackets and similar characters are treated literally.

powershell
if (Test-Path -LiteralPath $p) { Get-Content -LiteralPath $p }

Build paths with Join-Path

Let PowerShell pick the separator and avoid manual concatenation bugs.

powershell
$p = Join-Path (Join-Path $env:GITHUB_WORKSPACE 'dist') 'app.js'
Test-Path -LiteralPath $p

How to prevent it

  • Construct paths with Join-Path and use Test-Path -LiteralPath whenever a path may contain wildcard characters, so existence checks are reliable.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →