Is It There? Test-Path

Test-Path answers one question, is this file or folder there, with True or False, and it can also say whether a path is a file or a folder and whether any file matches a wildcard, so you can check before you act.

Apprentice · PowerShell · Operating System · Updated

After this lesson you can

  • Check whether a file or folder is there with Test-Path, and read its True or False answer
  • Tell a file from a folder with Test-Path -PathType Leaf or Container, and check for any match with a wildcard

Key takeaways

  • Test-Path answers True if a file or folder is there and False if it isn't, with no red error.
  • True and False are a Boolean, a yes-or-no answer. PowerShell writes them $true and $false.
  • -PathType Leaf asks "is it a file?" and -PathType Container asks "is it a folder?"
  • A wildcard such as photos\*.jpg asks whether anything matches at all.

Before you drive off for the weekend, you pat your pocket: keys? Phone? Wallet? You aren't packing anything yet, only checking. The same habit saves trouble at the prompt. Before you unzip a file a friend sent, is it really in Downloads? Before you zip into a folder, does that folder exist? A tutorial says "make sure package.json is there before you run this", and a script that backs up your homework should check that the homework folder is where it expects. Test-Path is PowerShell's pat on the pocket.

This lesson uses the road-trip folder and the trip-photos.zip from Many Files as One: Compress-Archive and Expand-Archive. If you don't have them, run that lesson's practice-folder lines and its Compress-Archive line first.

A yes-or-no question: Test-Path

Give Test-Path a path, and it answers with one word:

Set-Location "$HOME\terminal-practice\road-trip"
Test-Path trip-photos.zip
Test-Path passport.pdf
True
False

The zip is there; there's no passport.pdf. Notice what didn't happen: no red error for the missing file. Test-Path isn't trying to open anything, so a missing file isn't a problem to it. It's the answer to your question.

An answer that can only be True or False has a name: a Boolean (read "BOO-lee-un"). In PowerShell code you'll see the two answers written $true and $false (read "dollar true" and "dollar false"). The checks in this course, here and in the scripts of Module 6, all come down to one of them.

Any path works, as long as it's a full path or one from the folder you're in: Test-Path "$HOME\terminal-practice\road-trip\documents\packing-list.txt" answers True from anywhere.

A file or a folder? -PathType

Sometimes "is it there?" isn't enough: you need to know what it is. -PathType narrows the question. Container means a folder, a thing that holds other things. Leaf means a file, the way a leaf is the end of a branch on a tree:

Test-Path photos -PathType Container
Test-Path photos -PathType Leaf
Test-Path trip-photos.zip -PathType Leaf
True
False
True

photos is there, but it's a folder, so "is it a file?" gets False.

Is there any? Wildcards

A wildcard turns the question into "is there at least one?". * (read "star") stands for any letters, so *.jpg means "any name that ends in .jpg":

Test-Path photos\*.jpg
Test-Path documents\*.pdf
True
False

There are photos to share, and no PDFs among the documents.

Check before you act

Here's a mistake Test-Path would have caught. You want to zip the documents into an outbox folder, but you haven't made one yet:

Compress-Archive -Path documents -DestinationPath outbox\documents.zip
Test-Path outbox
Compress-Archive: The path 'C:\Users\Sam\terminal-practice\road-trip\outbox' either does not exist or is not a valid file system path.
False

Compress-Archive doesn't make the folder for you, and Test-Path confirms why it failed. Check first, then act:

New-Item -ItemType Directory -Path outbox
    Directory: C:\Users\Sam\terminal-practice\road-trip

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----           11/9/2026  4:01 AM                outbox

In a script, PowerShell can make that decision for you with if: "if this is True, do the first thing; otherwise (else), do the second". The braces, { }, hold each thing to do. Module 6 teaches if properly; here's a first look:

if (Test-Path outbox) { 'The outbox is ready.' } else { 'No outbox yet. Make it first.' }
The outbox is ready.

On Windows, capital letters don't matter, so Test-Path PHOTOS answers True. On a Mac or Linux they do, and the same line answers False. Windows PowerShell 5.1 gives the same answers as PowerShell 7 to everything in this lesson.

Try it. Is there a Downloads folder in your home folder? Ask with Test-Path "$HOME\Downloads" -PathType Container. Then ask whether it holds any .zip files.

Check yourself

2 questions to try on the lesson page.

So what

Look before you leap, in one word. Test-Path costs nothing, never changes a thing, and its True or False is what every careful command and script decides on.

Key terms

Cheat sheets

Related