---
title: "PowerShell Test-Path Cmdlet: Verify Files, Keys, and More"
description: "Master the PowerShell Test-Path cmdlet to confirm the existence of files, registry keys, and variables. Enhance your IT skills with this in-depth guide."
canonical: "https://adamtheautomator.com/powershell-test-path/"
---

# PowerShell Test-Path Cmdlet: Verify Files, Keys, and More

> Master the PowerShell Test-Path cmdlet to confirm the existence of files, registry keys, and variables. Enhance your IT skills with this in-depth guide.

Source: https://adamtheautomator.com/powershell-test-path/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![PowerShell Test-Path Cmdlet: Verify Files, Keys, and More](https://adamtheautomator.com/wp-content/uploads/2021/01/How-to-Use-the-PowerShell-Test-Path-Cmdlet.jpg)

# PowerShell Test-Path Cmdlet: Verify Files, Keys, and More

[![](https://secure.gravatar.com/avatar/d0b9d42e21e5622713f8b693aa5c0f9244d5f7dd200ed29b8398f52dee5de337?s=192&d=mm&r=g)Adam Bertram](https://adamtheautomator.com/author/adam-bertram/)20 January 20217 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[PowerShell](/tag/powershell/)

Table of Contents

*   [Prerequisites](#h-prerequisites)
*   [What Does the Test-Path Cmdlet Do?](#h-what-does-the-test-path-cmdlet-do)
*   [Test-Path Parameters and Usage](#h-test-path-parameters-and-usage)
*   [Path](#h-path)
*   [Using Wildcards](#h-using-wildcards)
*   [LiteralPath](#h-literalpath)
*   [PathType](#h-pathtype)
*   [Include](#h-include)
*   [Exclude](#h-exclude)
*   [Filter](#h-filter)
*   [NewerThan](#h-newerthan)
*   [OlderThan](#h-olderthan)
*   [IsValid](#h-isvalid)
*   [Credential](#h-credential)

If you need to validate a path to a file, registry key, certificate, or any other [_PowerShell drive_](https://docs.microsoft.com/en-us/powershell/scripting/samples/managing-windows-powershell-drives?view=powershell-7.2) path, you need the Test-Path cmdlet.

The Test-Path cmdlet is a simple yet useful way to quickly check many attributes of a file and other items. It can check whether a file exists (or other item types), a string is in the proper path format, or even whether or not an item is newer than or older than a specific time.

In this tutorial, you’re going to learn all about the PowerShell Test-Path cmdlet and how you can use it to improve your PowerShell scripts.

## Prerequisites

If you’d like to follow along with the examples in this tutorial, you’re going to need one thing; PowerShell. More specifically, the tutorial will be using [_PowerShell v7.03_](https://github.com/PowerShell/PowerShell/releases/tag/v7.0.3) on Windows 10, although many of the techniques you’ll learn will apply to older versions too.

## What Does the Test-Path Cmdlet Do?

The PowerShell Test-Path cmdlet is one of the simplest commands around. It’s a command that’s been in PowerShell since the beginning and only returns two values; True or False.

But, don’t let the simplicity fool you; it will save you _so_ much time in validating information in your PowerShell scripts.

Think of the PowerShell Test-Path cmdlet as quality control when working with PowerShell providers and drives. When writing a script, you’ll commonly work with various items contained in PowerShell drives. PowerShell drives like _C:\\,_ HKLM, _Cert_, and so on.

> _`Test-Path` does not work with all PS drives. If, for example, you attempt to use `Test-Path` against a registry key, it will work. If, you attempt to use `Test-Path` against a registry value, it will return `False` every time._

If you’re curious, run the `Get-PSDrive` cmdlet right now in PowerShel and notice all of the PS drives that show up for you.

![PS Drives after running Get-PSDrive](https://adamtheautomator.com/wp-content/uploads/2021/01/Untitled-2021-01-11T155642.180.png)

PS Drives after running Get-PSDrive

All of these drives have _paths_ inside of them like _C:\\Windows_, _HKLM:\\Software_, etc. When working with paths inside of your PowerShell scripts, it’s always good practice to first _test_ whether or not the path is valid or it exists it not. This is what PowerShell’s `Test-Path` does.

`Test-Path` defines a condition that returns `True` or `False` depending on if a specific condition is met (typically whether a file/folder, registry key, [certificate](https://adamtheautomator.com/x509-certificates/ "certificate"), or even a variable exists or not).

## Test-Path Parameters and Usage

Like many other PowerShell cmdlets, the `Test-Path` cmdlet has various parameters that change its behavior. Let’s now cover each parameter and demonstrate how it works and what kind of results you might expect to see.

### Path

The `Path` parameter is a mandatory parameter you will use with each `Test-Path` execution. The `Path` parameter defines the PSDrive’s path you’d like to test for the existence of.

If, for example, you’d like to test whether or not the folder _C:\\Foo_ exists, you’d provide the appropriate path to the `Path` parameter. Then, depending on if _C:\\Foo_ actually exists or not, `Test-Path` would either return `True` or `False`.

```powershell
PS> Test-Path -Path 'C:\Foo'
True
```

The same technique can be used for any item path as well. Maybe you’d like to test whether or not the registry key _HKLM:\\Software\\Foo_ exists. Simply use the registry key path with the `Path` parameter.

```powershell
PS> Test-Path -Path 'HKLM:\Software\Foo'
True
```

> _Know that all techniques demoed throughout this tutorial will work with any PowerShell drive path._

#### Using Wildcards

What happens if you don’t necessarily care if a path has a literal value. Instead, you’d just like to check whether or not a path matches a specific pattern. In that case, you can use wildcards in the `Path` value.

Perhaps you’d like to check whether or not your _C:\\Foo_ folder contains any subfolder that starts with _Bar_. In that case, you could use a wildcard.

```powershell
PS> Test-Path -Path 'C:\Foo\Bar*'
```

Once you execute the above command, `Test-Path` will check for the existence of _any_ folder that starts with _Bar_ and return `True` or `False` depending on if any folder matching that criteria exists or not.

Asterisks (`*`) match one _or more_ characters but you can also use question marks (`?`) too to get more granular and test for a single character.

Using the above scenario as an example, if the folder _C:\\Foo\\Bar1_ exists, you could test for any subfolder of _Foo_ that starts with _Bar_ and is exactly four characters using the command below.

```powershell
PS> Test-Path -Path 'C:\Foo\Bar?'
```

> _If, for some reason, you’d like to use the `Path` parameter using wildcards but want to literally match a wildcard character like `*`, you can always escape the wildcard characters with a backtick (\`)._

### LiteralPath

The `LiteralPath` parameter is nearly identical to the `Path` parameter with one exception; it doesn’t allow wildcards. Using the `LiteralPath` will interpret the path’s value _literally_.

For example, if you attempt to use an asterisk or question mark inside of the path value using `LIteralPath`, `Test-Path` will completely ignore the wildcard characters and test literally for _C:\\Foo\\Bar?_ like in the below example.

```powershell
PS> Test-Path -LiteralPath 'C:\Foo\Bar?'
```

> _You should use `LiteralPath` as the default path parameter if you don’t need to use any wildcard characters to ensure `Test-Path` tests the path you’re expecting._

### PathType

By default, when you run `Test-Path` and provide it a path, it will return `True` if it finds anything in that path. The item with a path could be a _container_ like a file folder, a registry key, certificate store, and so on, or a _leaf_ like a file, registry value, or certificate.

You can force `Test-Path` to get more granular and test specifically for a container or leaf item using the `PathType` parameter.

> _Test-Path uses the `PathType` value `Any`, by default._

If, for example, there’s a folder at _C:\\Foo\\Bar_ and you’re looking for a file at that path, you could use the `PathType` parameter as shown below. You want only to check if a file exists called _C:\\Foo\\Bar_.

```powershell
PS> Test-Path -LiteralPath 'C:\Foo\Bar' -PathType Leaf
```

Maybe instead, you need to confirm whether or not _C:\\Foo\\Bar_ is actually a file. In that case, you’d check for a container.

```powershell
PS> Test-Path -LiteralPath 'C:\Foo\Bar' -PathType Container
```

### Include

If using the `Path` parameter and wildcards, you’ll sometimes need to get more specific. In that case, you need to look into the `Include` and `Exclude` parameters.

Let’s say you have the following folders:

*   C:\\Foo\\Bar1
*   C:\\Foo\\Bar2
*   C:\\Foo\\Bar3

You’d like to check whether any folder that starts with _Bar_ exists that are exactly four characters like _Bar1_, _Bar2_, etc.

```powershell
PS> Test-Path -Path C:\Foo\Bar? -PathType Container
```

The above command works fine, but now you’d like to only find folders in _C:\\Foo_ named _Bar2_. In that case, you could use the `Include` parameter.

```powershell
PS> Test-Path -Path C:\Foo\Bar? -PathType Container -Include 'Bar2'
```

The above command now only tests for essentially a single folder _C:\\Foo\\Bar2_. You’d probably be better off just using `Test-Path -Path 'C:\Foo\Bar2' -PathType Container` instead.

### Exclude

The `Exclude` parameter works similarly to the `Include` parameter except this time it _excludes_ paths matching a string.

Maybe you want to ensure there is at least one file inside of the _C:\\Foo_ folder, and you use the following command:

```powershell
PS> Test-Path -Path C:\Foo\* -PathType Leaf
```

The above command returns `True` or `False` if any files are in _C:\\Foo_. But maybe you want to ensure any files _besides_ ones with a file extension of `txt` exist. In that case, you could use the `Exclude` parameter using a wildcard to exclude all files with the `txt` extension from testing.

```powershell
PS> Test-Path -Path C:\Foo\* -PathType Leaf -Exclude *.txt
```

### Filter

According to Microsoft’s documentation, the `Filter` parameter “specifies a filter in the format or language of the provider. The value of this parameter qualifies as the `Path` parameter. The syntax of the filter, including the use of wildcard characters, depends on the provider”.

Although the `Filter` parameter should be used with other cmdlets like `Get-Childitem`, for example, it is rarely if ever used with `Test-Path`. If you’ve found a good use for the `Filter` parameter, please reach out on Twitter at @adbertram.

**_Related: [Get-ChildItem: Listing Files, Registry, Certificate and More as One](https://adamtheautomator.com/get-childitem/)_**

### NewerThan

Have you ever needed to check the timestamp on a file and make a decision based on that? If so, the `NewerThan` and `OlderThan` parameters save a lot of code. The `NewerThan` parameter checks whether or not an item’s timestamp is newer than a specific date.

The `NewerThan` parameter accepts a string or a DateTime object to represent a timestamp to check against. For example, to check whether or not the file _C:\\Foo\\bar.txt_ was created after January 20th, 2021, you’d run `Test-Path` like below.

```powershell
Test-Path -LiteralPath 'C:\Foo\bar.txt' -NewerThan 'January 20, 2021'
## or
Test-Path -LiteralPath 'C:\Foo\bar.txt' -NewerThan '1/20/21'
```

### OlderThan

The `OlderThan` parameter is exactly the same as the `NewerThan` parameter but opposite. This parameter checks whether or not an item is _older_ than a specific date.

```powershell
Test-Path -LiteralPath 'C:\Foo\bar.txt' -OlderThan 'January 20, 2021'
## or
Test-Path -LiteralPath 'C:\Foo\bar.txt' -OlderThan '1/20/21'
```

### IsValid

If you’ve ever dynamically built a path in a script, you’d know the struggle. Sometimes you may fat-finger a key or somehow get some special character in a path; if so, the `IsValid` parameter is for you.

The `IsValid` parameter is a unique parameter that turns `Test-Path`, not into a cmdlet that checks for an item’s existence but one that checks for path syntax. This parameter confirms whether or not a path is valid _only_.

For example, perhaps you need to verify whether a path is syntactically valid. You’re working with a couple of variables and concatenate them to build a path.

> _When concatenating paths, always use the `Join-Path` cmdlet. This is just for example purposes only!_

```powershell
$someVar = 'abc:dff'
$rootPath = 'C:\'
$path = "$someVar$rootPath
```

Now to ensure the path you’ve dynamically created is valid, use the `IsValid` parameter like below. You’d find that `Test-Path` returns `False`.

```powershell
PS> Test-Path -LiteralPath $path -IsValid
False
```

The path `abc:dffC:\` is not a valid path now allowing you to create a validation routine from this situation.

> _If you’re using PowerShell v6.1.2 or earlier and are using the `IsValid` and `PathType` parameters together, `Test-Path` will ignore the `PathType` parameter._

### Credential

Even though you’ll find the `Credential` parameter on `Test-Path`, you may think you can use it to authenticate to PS drives as another user. That’s a valid assumption, but it’s wrong.

Unfortunately, the `Credential` parameter doesn’t do much with the `Test-Path` cmdlet. Microsoft recommends using the `Invoke-Command` cmdlet and using the `Credential` parameter there if you want to invoke `Test-Path` with alternate credentials.

```powershell
Invoke-Command -Credential (Get-Credential) -Scriptblock {Test-Path -LiteralPath 'C:\'}
```

**_Related: [Invoke-Command: The Best Way to Run Remote Code](https://adamtheautomator.com/invoke-command/)_**

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-test-path%2F&text=PowerShell%20Test-Path%20Cmdlet%3A%20Verify%20Files%2C%20Keys%2C%20and%20More)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-test-path%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-test-path%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2026/06/26995-troubleshoot-dns-issues-powershell-codex.webp)

### [Troubleshoot DNS Issues with PowerShell](/troubleshoot-dns-issues-powershell/)

Troubleshoot DNS issues with PowerShell by testing name resolution, DNS client settings, cache entries, and network connectivity in a repeatable workflow.

![](https://adamtheautomator.com/wp-content/uploads/2025/10/image_2025-10-24_095322075.png)

### [Migrating from PowerShell 6 to 7.5: Breaking Changes/New Features](/migrating-powershell-6-to-7-5/)

Migrate from PowerShell Core 6 to 7.5: breaking changes, features, and testing tips.

![](https://adamtheautomator.com/wp-content/uploads/2025/06/featured-image-6.png)

### [How to Add Timeouts to Pester Tests with PowerShell Runspaces](/pester-test-timeout-runspaces/)

Prevent Pester tests from hanging indefinitely using PowerShell runspaces. Learn to handle variable scoping, module loading, TestDrive access, and stream capture challenges with timeout protection.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
