Test-Path

Verb: TestChapter 18: Handle errors and finish

Tests whether a path exists, and returns True or False.

Syntax

Short syntax for this command. The parameter table below may list more parameters than this short line shows.

Syntax
Test-Path -Path <Path>

Parameters

Note: the short syntax above does not show every parameter in this table. The rows marked No in the last column are still listed here with their descriptions. Check Get-Help for the full syntax before you use one of them.

ParameterWhat it doesIn short syntax
-Paththe path to testYes
-PathTypecheck for a file or a container, such as a folderNo

Examples

Returns True if the file exists and False if it does not. Test first, so your script can handle a missing file cleanly.

Example 1
Test-Path -Path "C:\Scripts\report.ps1"

Illustrative output: see the lesson example for this command.

Common mistakes

General cautions for this command.

  • Guessing parameters instead of checking Get-Help for this command.
  • Running a changing form of this command before previewing its effect.

Related commands

Commands that share this command's lesson come first, then commands that share its verb. Every link goes to a real command page.

Related lessons