Your script checks for a config file before it reads it. Test-Path says True. One line later, Get-Item on the very same path fails with "Could not find item". Same folder, same file name, two answers.
Nothing is broken. Test-Path and Get-Item answer different questions, and they treat hidden files, square brackets, wildcards and empty variables differently. This tip shows each difference with real output, and the one-call pattern I use when I need the file object anyway. If you mostly delete or create things after a check, pair this with safe deletes with Remove-Item and creating folders with New-Item.
The one-line difference
-
Test-Pathanswers "is something there?" It returnsTrueorFalse, and a missing path is not an error. -
Get-Itemanswers "give me the thing." It returns the file or folder object (name, size, dates, attributes), and a missing path is an error.
Use Test-Path for an if. Use Get-Item when you need the object. The trouble starts when you assume both see the same files.
Set up a safe demo folder
This creates a throwaway folder in your temp directory with a hidden .env file, a file with brackets in its name, a look-alike file without them, and a logs folder:
$demo = Join-Path ([IO.Path]::GetTempPath()) 'testpath-getitem-demo'
New-Item -ItemType Directory -Path $demo -Force | Out-Null
Set-Location -Path $demo
Set-Content -Path .\.env -Value 'API_URL=https://example.test' -Force
Set-Content -LiteralPath '.\report[1].txt' -Value 'Q1 final'
Set-Content -Path .\report1.txt -Value 'Q1 draft'
New-Item -ItemType Directory -Path .\logs -Force | Out-Null
Set-Content -Path .\logs\a.log -Value 'old run'
Set-Content -Path .\logs\b.log -Value 'new run'
# On Windows, mark .env as Hidden (on Linux and macOS the dot already hides it)
if ($env:OS -eq 'Windows_NT') {
$f = Get-Item -Path .\.env -Force
$f.Attributes = $f.Attributes -bor [IO.FileAttributes]::Hidden
}
Trap 1: hidden files (Test-Path sees them, Get-Item does not)
Test-Path -Path .\.env
Get-Item -Path .\.env
Output (path shortened):
True
Get-Item: Could not find item ...\testpath-getitem-demo\.env.
Test-Path reports hidden items. Get-Item skips them unless you add -Force, which the docs describe as getting "items that can't otherwise be accessed, such as hidden items":
(Get-Item -Path .\.env -Force).Name
Output:
.env
If a script checks with Test-Path and then reads with Get-Item, give the Get-Item call -Force or the two will disagree on every hidden file.
Trap 2: square brackets (Test-Path answers about the wrong file)
-Path treats [1] as a wildcard meaning "one character from this set". So report[1].txt matches report1.txt:
Test-Path -Path '.\report[1].txt'
(Get-Item -Path '.\report[1].txt').Name
Get-Content -Path '.\report[1].txt'
Output:
True
report1.txt
Q1 draft
You asked for the final report and got the draft. Both cmdlets were fooled the same way. -LiteralPath turns the wildcard reading off:
Test-Path -LiteralPath '.\report[1].txt'
(Get-Item -LiteralPath '.\report[1].txt').Name
Get-Content -LiteralPath '.\report[1].txt'
Output:
True
report[1].txt
Q1 final
Now delete the bracket file and ask again:
Remove-Item -LiteralPath '.\report[1].txt'
Test-Path -Path '.\report[1].txt'
Test-Path -LiteralPath '.\report[1].txt'
Output:
True
False
With -Path, Test-Path still says True because report1.txt is there. Only -LiteralPath tells you the file you meant is gone. Any time a path comes from a variable, a CSV or a user, use -LiteralPath for both cmdlets.
Trap 3: wildcards (True for one, objects for all)
Test-Path -Path .\logs\*.log
(Get-Item -Path .\logs\*.log).Count
Test-Path -Path .\logs\*.tmp
Output:
True
2
False
Test-Path with a wildcard means "does at least one match exist?" Get-Item returns every match, so code that expects one object gets an array. That is handy (Test-Path .\logs\*.log is a quick "is there anything to process?") as long as you know it is happening.
File or folder?
A plain Test-Path is True for both. Ask for the type with -PathType, or read it off the object:
Test-Path -Path .\logs
Test-Path -Path .\logs -PathType Leaf
Test-Path -Path .\logs -PathType Container
(Get-Item -Path .\logs).PSIsContainer
Output:
True
False
True
True
-PathType Leaf means "a file", Container means "a folder". This is the check to run before Get-Content, which fails on a folder.
Trap 4: an empty variable
$path = '' # a config value nobody filled in
Test-Path -Path $path
Get-Item -Path $path
Output:
False
Get-Item: Cannot bind argument to parameter 'Path' because it is an empty string.
In PowerShell 7, Test-Path quietly returns False for an empty string, which hides the real bug: the variable was never set. $null is not quiet:
$path = $null
Test-Path -Path $path
Output:
Test-Path: Value cannot be null. (Parameter 'The provided Path argument was null or an empty collection.')
If a path comes from config, check that it is set (if (-not $path) { throw 'path is not set' }) before you test whether it exists.
When you need the object: skip Test-Path
if (Test-Path $p) { $f = Get-Item $p } looks at the disk twice, and the two checks can disagree (hidden files, brackets) or the file can disappear between them. When you need the object, ask once:
$item = Get-Item -LiteralPath .\.env -Force -ErrorAction SilentlyContinue
if ($item) { "found $($item.Name)" } else { '.env is missing' }
Output:
found .env
When a missing file should take a different code path, make the error terminating and catch exactly "not found":
try {
$cfg = Get-Item -LiteralPath .\settings.json -ErrorAction Stop
"using $($cfg.Name)"
}
catch [System.Management.Automation.ItemNotFoundException] {
'settings.json is missing, using defaults'
}
Output:
settings.json is missing, using defaults
Other errors, like access denied, still surface instead of being treated as "missing".
Something only Test-Path does: age checks
For files, Test-Path can filter by last write time with -OlderThan and -NewerThan:
(Get-Item -Path .\logs\a.log).LastWriteTime = (Get-Date).AddDays(-45)
Test-Path -Path .\logs\a.log -OlderThan (Get-Date).AddDays(-30)
Test-Path -Path .\logs\b.log -OlderThan (Get-Date).AddDays(-30)
Output:
True
False
Good for "is the backup older than 30 days?" alerts without loading the object.
Quick reference
| You want | Use |
|---|---|
| Yes/no: does it exist? | Test-Path -LiteralPath $p |
| Is it a file? A folder? |
Test-Path -LiteralPath $p -PathType Leaf / Container
|
| The object (size, dates, attributes) | Get-Item -LiteralPath $p |
| The object, including hidden files | Get-Item -LiteralPath $p -Force |
The object or $null, one disk check |
Get-Item -LiteralPath $p -Force -ErrorAction SilentlyContinue |
| "Missing" as its own code path | try { Get-Item ... -ErrorAction Stop } catch [System.Management.Automation.ItemNotFoundException] { } |
| Any match for a pattern? | Test-Path -Path .\logs\*.log |
| Older than N days? | Test-Path $p -OlderThan (Get-Date).AddDays(-N) |
I tested every block above in PowerShell 7.4.6. The demo ran on Linux, where a leading dot is what makes .env hidden; on Windows the setup block sets the Hidden attribute instead, and the paths look like C:\.... Docs: Test-Path on Microsoft Learn, Get-Item on Microsoft Learn, and about_Wildcards.
Related tips: delete files in PowerShell with Remove-Item, safely, create directories in PowerShell with New-Item, Test-Path basics: check a path before you act, and Get-ChildItem -Filter vs -Include, the other place wildcards bite.
Practice it hands-on
Path checks feel obvious until a hidden file or a bracket in a name breaks one. CMD Master's free interactive PowerShell, CMD and Bash terminal lessons run in your browser, so you can practice file and folder commands without risking real files.
Your turn: what does this output?
Set-Content -Path .\data1.csv -Value 'id,name'
Test-Path -Path '.\data[12].csv'
Test-Path -LiteralPath '.\data[12].csv'
(Get-Item -Path '.\data[12].csv').Name
Three lines, three answers. Drop them in the comments before you run it.
Bonus: if you still use Windows PowerShell 5.1, does Test-Path -Path '' give you False like PowerShell 7 does, or something else? I'd like to hear what you see.
Top comments (0)