Check if a File Exists in PowerShell with Test-Path and LiteralPath

  • Published: 2026.10.09
  • Updated: 2026.10.09
  • Windows

A file can be visible in File Explorer while a PowerShell command fails to find it. A script can also find a folder when it expects a file. Checking the path and the item type first makes those mistakes easier to spot.

In Windows PowerShell 5.1 and PowerShell 7, Test-Path can check files and folders on the file system. The examples below show how to handle square brackets in names, distinguish a file from a folder, and interpret the result for a missing file.


Check whether a file exists

Open PowerShell in the folder containing the file, and replace .\sample[1].txt with its name. The .\ prefix makes this a path relative to your current location. If you are working in a different folder, use the file’s full path in single quotes to make the location clear.

Test-Path -LiteralPath '.\sample[1].txt' -PathType Leaf

When that file exists, the result is:

True

True means a file was found at the specified path. A False result can mean the name or location is wrong, the file is missing, or the item is a different type. Start with the spelling, extension, and the folder where PowerShell is currently working.

Distinguish files from folders

For file-system paths, use -PathType Leaf for a file and -PathType Container for a folder. Suppose your current folder contains a directory named data:

Test-Path -LiteralPath '.\data' -PathType Leaf
Test-Path -LiteralPath '.\data' -PathType Container
False
True

The first result is false because data is a folder, not a file. The second is true. Without -PathType, either type can satisfy the existence check. Adding Leaf makes a check before processing a file more specific.

Use LiteralPath for names with square brackets

-Path accepts wildcard patterns. The [1] portion describes a character choice rather than literal brackets. If the folder contains sample[1].txt but no sample1.txt, these commands give different results:

Test-Path -Path '.\sample[1].txt' -PathType Leaf
Test-Path -LiteralPath '.\sample[1].txt' -PathType Leaf
False
True

-LiteralPath treats the brackets as part of the name. Quoting the string and disabling wildcard interpretation are separate concerns: adding single quotes to -Path does not remove this difference. When you mean one specific file, -LiteralPath is a convenient choice.

Keep IsValid separate from existence checks

-IsValid asks a different question from whether a file exists. If there is no missing.txt in your current folder, the following example still produces a true result on the second line:

Test-Path -LiteralPath '.\missing.txt' -PathType Leaf
Test-Path -LiteralPath '.\missing.txt' -IsValid
False
True

That second True does not prove the file exists. To check for a file, use -LiteralPath with -PathType Leaf and leave out -IsValid.

Use the result in an if statement

The result is a Boolean, not a string, so it can go directly into an if condition. This example gives a different message depending on whether the file is found:

$file = '.\sample[1].txt'
if (Test-Path -LiteralPath $file -PathType Leaf) {
    Write-Output 'File found'
} else {
    Write-Output 'Check the file path'
}

With sample[1].txt present, the message is:

File found

The check does not change the file’s contents. Finding the file does not guarantee permission to read it, validate its contents, or keep it available for the next command. Another process could move or delete it after the check. If the command that actually reads it fails, use that error to decide what to do next.

Once you have found the file, you can compare its SHA256 hash with Get-FileHash when you need to check whether its contents match another copy or a published checksum.

References: Microsoft Learn: Test-Path and PowerShell wildcard characters.

このブラウザからの自分のアクセスを解析から除外できます。