Skip to content

/errors/powershell/cannot-find-path-because-it-does-not-exist

PowerShell: Cannot find path 'C:\...' because it does not exist.

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash)updated
tool
PowerShell
exit code
1
versions
Windows PowerShell 5.1, PowerShell 7.x
os
windows, linux, macos

Cannot find path 'C:\work\data.csv' because it does not exist. is PowerShell's ItemNotFoundException: the provider (file system, registry, certificate store) has no item at that path. The quoted path is the resolved one, so compare it with where the file really is. The two non-obvious causes are relative paths resolved against the current location instead of the script folder, and file names with [ ], which -Path treats as wildcards.

Reproduce it

powershell
$ErrorActionPreference = 'Stop'
try { Get-Item -Path missing.txt } catch { $_.Exception.Message; '  -> ' + $_.FullyQualifiedErrorId }
"Test-Path: $(Test-Path -Path missing.txt)"
Set-Content -LiteralPath 'report[1].txt' -Value 'x'
try { Get-Content -Path 'report[1].txt' } catch { $_.Exception.Message }
Get-Content -LiteralPath 'report[1].txt'

Output

Cannot find path '/tmp/work/missing.txt' because it does not exist.
  -> PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand
Test-Path: False
An object at the specified path report[1].txt does not exist, or has been filtered by the -Include or -Exclude parameter.
x

The bracket case does not even report "cannot find path": -Path treats [1] as a wildcard character class that matches report1.txt, finds nothing, and prints a different message. -LiteralPath uses the name as written.

Relative paths in scripts

A relative path is resolved against the current location (Get-Location), which is wherever the user or scheduler started PowerShell, not the script's folder. Build paths from $PSScriptRoot:

powershell
$csv = Join-Path $PSScriptRoot 'data\input.csv'
if (-not (Test-Path -LiteralPath $csv)) { throw "Input file not found: $csv" }
Import-Csv -LiteralPath $csv

.NET methods ([IO.File]::ReadAllText('data.txt')) resolve relative paths against the process working directory, which Set-Location does not change. Pass full paths to .NET methods, for example (Resolve-Path data.txt).Path or "$PWD\data.txt".

Other situations

SituationFix
Unquoted path with spacesGet-Item 'C:\Program Files\App'
Mapped drive in a scheduled taskuse \\server\share\path; drive mappings are per logon session
Registry pathuse the provider drive, HKLM:\SOFTWARE\..., not HKEY_LOCAL_MACHINE\...
Path built from an empty variablecheck the variable first; Join-Path with an empty part gives a wrong path

Sources