PowerShell: How to Use Test-Path to Check Multiple Paths


You can use the Test-Path cmdlet in PowerShell to check if a particular folder or file exists at a specific path.

You can use the following methods with the Test-Path cmdlet to check if multiple paths exist:

Method 1: Check if Multiple Paths Exist

Test-Path c:\users\bobbi\first, c:\users\bobbi\second

Method 2: Check if All Paths Exist

(Test-Path $first_path) -and (Test-Path $second_path)

Method 3: Check if At Least One of Several Paths Exists

(Test-Path $first_path) -or (Test-Path $second_path)

The following examples show how to use each of these methods in practice.

Example 1: How to Use Test-Path to Check Multiple Paths in PowerShell

Suppose that we would like to know if each of the following paths exists on our computer:

  • c:/users/bobbi/data1
  • c:/users/bobbi/data7

We can use the following syntax with the Test-Path cmdlet to check:

Test-Path c:\users\bobbi\data1, c:\users\bobbi\data7

The following screenshot shows how to use this syntax in practice:

PowerShell Test-Path multiple paths

The output returns True for the first path, which tells us that it exists.

The output then returns False for the second path, which tells us that it does not exist.

Example 2: How to Use Test-Path to Check if All Paths Exist in PowerShell

Suppose that we would like to know if all of the following paths exists on our computer:

  • c:/users/bobbi/data1
  • c:/users/bobbi/data7

We can use the following syntax with the Test-Path cmdlet to check:

$first_path = "c:\users\bobbi\data1"
$second_path = "c:\users\bobbi\data7"

(Test-Path $first_path) -and (Test-Path $second_path)

The following screenshot shows how to use this syntax in practice:

PowerShell Test-Path to check if multiple paths exist

The output returns False, which tells us that both of these paths do not exist.

Example 3: How to Use Test-Path to Check if At Least One of Several Paths Exists in PowerShell

Suppose that we would like to know if all of the following paths exists on our computer:

  • c:/users/bobbi/data1
  • c:/users/bobbi/data7

We can use the following syntax with the Test-Path cmdlet to check:

$first_path = "c:\users\bobbi\data1"
$second_path = "c:\users\bobbi\data7"

(Test-Path $first_path) -or (Test-Path $second_path)

The following screenshot shows how to use this syntax in practice:

PowerShell Test-Path to check if at least one path exists

The output returns True, which tells us that at least one of these paths exists.

Note: You can find the complete documentation for the Test-Path cmdlet in PowerShell here.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Use Test-Path with Wildcard Characters
PowerShell: How to Check if Folder Exists
PowerShell: How to Delete File if it Exists
PowerShell: How to Find Empty Folders

Leave a Reply