PowerShell Test-Path [With Examples]

Today, I will explain how to work with test-path in PowerShell. As a developer, you mostly use this cmdlet when working with files and folders in PowerShell. In this tutorial, I will explain the PowerShell test-path with various real examples.

To check if a specific path exists in PowerShell, use the Test-Path cmdlet. By executing Test-Path -Path “C:\MyFolder”, you can quickly determine if the specified file or directory exists, with the cmdlet returning $true if it does and $false if it doesn’t.

What is the Test-Path PowerShell cmdlet?

The Test-Path cmdlet in PowerShell is used to determine whether all elements of a specified path exist. It returns $true if the path exists and $false if it does not. This cmdlet is used to check files, directories, registry keys, and even more complex paths.

Syntax of Test-Path

Below is the syntax of Test-Path:

Test-Path -Path <String> [-PathType <String>] [-Credential <PSCredential>] [-Exclude <String[]>] [-Filter <String>] [-Include <String[]>] [-IsValid] [-LiteralPath <String[]>] [-UseTransaction] [<CommonParameters>]

Parameters:

  • -Path: Specifies the path to be tested.
  • -PathType: Specifies the type of the path (Container, Leaf, or Any).
  • -Credential: Specifies a user account that has permission to perform this action.
  • -Exclude: Specifies items that this cmdlet omits.
  • -Filter: Specifies a filter in the provider’s format or language.
  • -Include: Specifies items that this cmdlet includes.
  • -IsValid: Determines whether the syntax of the path is valid.
  • -LiteralPath: Specifies a path to be tested exactly as typed.

Check out Add Date to Filename Using PowerShell

PowerShell Test-Path Examples

Now, let me show you a few real examples of using the PowerShell Test-Path cmdlet.

Example-1: Check File Existence

To check if a file exists, you can use the following command:

Test-Path -Path "C:\MyFolder\Report2024.pdf"

This command will return $true if the file Report2024.pdf exists in the specified directory.

I executed the above PowerShell script using VS code. Since the file does not exist, it returns me as False. Here is the screenshot for your reference.

PowerShell Test-Path

Example-2: Check Directory Existence

Similarly, to check if a directory exists, you use:

Test-Path -Path "C:\MyNewFolder"

This will return $true if the directory MyNewFolder exists under C drive.

Here is the exact output in the screenshot below:

Test-Path in PowerShell

Read Sort Files by Date in PowerShell

Example-3: Using PathType Parameter

The -PathType parameter of the Test-Path cmdlet allows you to specify whether you are checking for a file (Leaf) or a directory (Container):

Test-Path -Path "C:\Users\Bijay\Documents" -PathType Container
Test-Path -Path "C:\Users\Bijay\Documents\Report2024.pdf" -PathType Leaf

These commands ensure that you are explicitly checking for a directory or a file, respectively.

Example-4: Check Multiple Paths

By using the Test-Path PowerShell cmdlet, you can also check multiple paths using a loop or an array. Here is an example and the complete PowerShell script.

Here is the complete PowerShell script.

$paths = @("C:\MyNewFolder\Report2024.pdf", "C:\MyNewFolder\MyPDFFile.pdf")
foreach ($path in $paths) {
    if (Test-Path -Path $path) {
        Write-Output "$path exists."
    } else {
        Write-Output "$path does not exist."
    }
}

This script will iterate through each path in the array and check its existence, outputting the result.

You can see the exact output in the screenshot below:

PowerShell Test-Path Examples

Check out Get Unique Lines from a File Using PowerShell

Example-5: Use Filters with Test-Path

You can use the -Filter parameter to narrow down the search. For example, to check for any .pdf files in a directory; you can use the Test-Path like the below script.

Test-Path -Path "C:\MyNewFolder\*.pdf"

This will return $true if there are any .pdf files in the Documents directory.

Example-6: Validate Path Syntax

To validate the syntax of a path without checking its existence, use the -IsValid parameter:

Test-Path -Path "C:\Users\Bijay\Documents\InvalidPath::" -IsValid

This will return $false if the path syntax is invalid.

Check out PowerShell Read-Host

Example-7: Automate Daily Checks

Let me show you another real example.

Imagine you are an IT administrator in New York, and you need to check the existence of daily reports in a shared directory.

Here is the complete PowerShell script that you can use.

$reportPath = "C:\Company\Reports\Daily\Report_$(Get-Date -Format 'yyyyMMdd').pdf"
if (Test-Path -Path $reportPath) {
    Write-Output "Today's report exists."
} else {
    Write-Output "Today's report is missing."
}

This script checks for the existence of a report named with today’s date. If the file exists, it confirms; otherwise, it alerts you that the report is missing.

Conclusion

The Test-Path cmdlet is very useful for system administrators and IT professionals while working with files and directories. You can use the PowerShell Test-Path cmdlet to check for files, directories or folders, registry keys, etc. I have shown some real examples of the PowerShell Test-Path cmdlet in this tutorial.

If you still have questions, leave a comment below.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.