/errors/powershell/cannot-find-path-because-it-does-not-exist
PowerShell: Cannot find path 'C:\...' because it does not exist.
- 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
$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:
$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
| Situation | Fix |
|---|---|
| Unquoted path with spaces | Get-Item 'C:\Program Files\App' |
| Mapped drive in a scheduled task | use \\server\share\path; drive mappings are per logon session |
| Registry path | use the provider drive, HKLM:\SOFTWARE\..., not HKEY_LOCAL_MACHINE\... |
| Path built from an empty variable | check the variable first; Join-Path with an empty part gives a wrong path |
Sources
- learn.microsoft.comlearn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-item
- learn.microsoft.comlearn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/test-path
- learn.microsoft.comlearn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_wildcards