Check if a file or folder exists with Test-Path

The PowerShell true/false existence test every script should start with — supports files, folders, registry keys, and wildcards.

WindowsFilesscriptingpowershellLow risk
Run Test-Path '<path>' — it returns True if the item exists, False if not; use in if (Test-Path ...) guards.

01Command

Command
$ Test-Path 'C:\Program Files\Git\bin\git.exe'

02What it does

Test-Path works on any PowerShell provider path: filesystem, registry (HKLM:\...), even environment variables. -PathType Leaf restricts the check to files and -PathType Container to folders, so you don't mistake a directory for the file you want. -IsValid checks only whether the path string is syntactically valid without touching the disk.

03Risk

RiskLow risk
RequirementsPowerShell
!Warning: Test-Path returns False for a path you can see but lack permission to probe on some network shares — don't treat False as a guarantee the item is gone.

04Undo

✓No undo needed — this command does not change persistent state.

05Sources

  • Test-Path | Microsoft Learn
    Microsoft · vendor-doc · 2026-09-17
    Evidence: Microsoft documents Test-Path returning $True/$False for an existing path, plus -PathType Leaf/Container and -IsValid syntax checks.
  • Test-Path - PowerShell
    SS64 · technical · 2026-09-17
    Evidence: SS64 shows Test-Path examples including if (Test-Path ...) guards and the -IsValid parameter for validating path syntax.

Last verified: 2026-09-17 · Category: Files & Folders

06Related solutions