How to Check Whether a File or Folder Exists in PowerShell with Test-Path
Test-Path tells you whether a file or folder exists: it answers True or False. This guide checks a file, a folder and a missing name, tells files from folders, and acts on the answer with if, with real output from PowerShell 7.6.10.
The Test-Path page: "The Test-Path cmdlet determines whether all elements of the path exist. It returns $true if all elements exist and $false if any are missing."
Syntax
Test-Path <path>
Test-Path <path> -PathType <type>
Test-Path -LiteralPath <path>
| Part | What it means |
|---|---|
<path> | The file or folder to look for: a name in the current folder, or a full path. Quote it when it has a space |
-PathType <type> | Optional: Leaf answers True only for a file, Container only for a folder |
-LiteralPath <path> | Uses the path exactly as typed, for names that contain square brackets |
Whatever the path, the answer is one of two values. The page: "The cmdlet returns a Boolean value."
The Example
The example works in a folder named test-path-demo that holds a file, notes.txt, and a folder, photos. Step 1 makes them. The commands run in the PowerShell 7.6.10 console on a Mac.
Prerequisites
- PowerShell 7. This guide uses PowerShell 7.6.10:
$PSVersionTable.PSVersion.ToString()printed7.6.10.
Step 1: Make a File and a Folder to Check
Set-Content -Path notes.txt -Value 'buy milk'
New-Item -ItemType Directory -Path photos | Out-Null
Neither command printed anything. Set-Content wrote the text buy milk into a new file, notes.txt. The New-Item page: "The ItemType parameter specifies that the new item is a directory, not a file or other file system object." So New-Item made the folder photos, and the pipe sent what it returned to Out-Null, which, as the Out-Null page puts it, "Hides the output instead of sending it down the pipeline or displaying it."
Step 2: Check Whether a File or Folder Exists
Test-Path notes.txt
Test-Path photos
Test-Path missing.txt

True
True
False
Test-Path returned True for the file and for the folder, which both exist, and False for missing.txt, which doesn't. A name on its own is looked up in the current folder, here test-path-demo.
Step 3: Check for a File or a Folder Only
Test-Path notes.txt -PathType Leaf
Test-Path photos -PathType Leaf
Test-Path photos -PathType Container
True
False
True
Without -PathType, Test-Path returned True for both kinds of item. With it, the item must also be of that kind: photos exists, but it's a folder, so -PathType Leaf returned False. The Test-Path page lists the values: "Container - An element that contains other elements, such as a directory or registry key." And: "Leaf - An element that doesn't contain other elements, such as a file."
Step 4: Act on the Answer with if
if (Test-Path notes.txt) { 'notes.txt is here' } else { 'notes.txt is missing' }
if (Test-Path missing.txt) { 'missing.txt is here' } else { 'missing.txt is missing' }
notes.txt is here
missing.txt is missing

if runs the test in parentheses. When it's True, the code in the first pair of braces runs; when it's False, the code after else runs. Text in quotes on its own is printed, so each line printed one message. The about_If page: "You can use the if statement to run code blocks if a specified conditional test evaluates to true."
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
A path with a space and no quotes: Test-Path my notes.txt | Test-Path: A positional parameter cannot be found that accepts argument 'notes.txt'. | Quote the path: Test-Path 'my notes.txt' returned True |
A misspelled variable: Test-Path $fiel | Test-Path: Value cannot be null. (Parameter 'The provided Path argument was null or an empty collection.'): $fiel was never set | Check the variable's name |
Square brackets in a file name: Test-Path 'notes[1].txt' | False, though the file exists | Use -LiteralPath: Test-Path -LiteralPath 'notes[1].txt' returned True |
The Test-Path page covers the first row: "If the path includes spaces, enclose it in quotation marks." The about_Wildcards page explains the last: "The square-bracket ([]) characters are wildcards so they must be escaped when trying to get the file using one of the Item cmdlets, such as Get-Item." -LiteralPath avoids that. The Test-Path page: "Unlike Path, the value of the LiteralPath parameter is used exactly as it's typed. No characters are interpreted as wildcard characters."
FAQ
Can I check a full path?
Yes: Test-Path $HOME returned True, because $HOME holds the full path of your home folder. Every folder along the path must exist. The page explains its own example: "This command checks whether all elements in the path exist, including the C: directory, the Documents and Settings directory, and the DavidC directory. If any are missing, the cmdlet returns $false."
Can I check several paths at once?
Yes, with commas between them: Test-Path notes.txt, missing.txt printed True and then False, one answer for each path, in order.
What does Test-Path return for an empty path?
Test-Path '' returned False. The page: "If the Path is a whitespace or empty string, then the cmdlet returns $false. If the Path is $null, an array of $null or an empty array, the cmdlet returns a non-terminating error." That error is the one in the second row of Common Mistakes.
Conclusion
Test-Path notes.txt returned True and Test-Path missing.txt returned False. Add -PathType Leaf or -PathType Container to check for a file or a folder only, put the check in an if to act on it, and use -LiteralPath for names with square brackets.
Additional Resources
- Test-Path — every parameter, including PathType and LiteralPath, with examples.
- about_If — running code only when a test is true.
- about_Wildcards — the wildcard characters a path can hold, and how to use them literally.
