The Test-Path cmdlet is used to check whether a specific file, folder, or registry item exists. The command will return True if the object exists and is accessible at the specified path. If at least one element of the path does not exist, it returns False.
Check if File or Folder Exists
For example, let’s verify the existence of the following folder path (quotation marks are required for any path that contains spaces or special characters):
Test-Path -Path 'C:\Program Files\'

Check for specific path element
The command checks the path and file by default. Use the PathType parameter to check for a specific path element. Available values:
- Container โ checks the existence of a parent item, whether it is a regular directory or a registry key.
- Leaf โ a file or other element that does not contain sub-items.
- Any โ either a container or a leaf
Verify if a PowerShell profile file (Microsoft.PowerShell_profile.ps1) has been created for the current user:
Test-Path -Path $PROFILE -PathType Leaf

Test-Path command supports wildcards
The Test-Path command supports wildcards, which allow you to test for items whose exact names are unknown. For example, let’s check if the specified folder contains at least one .TXT file:
Test-Path -PathType Leaf -Path "C:\LOGS\*.txt"

Adding If/Else
The Test-Path cmdlet is commonly used in PowerShell scripts to check for the existence of a file or folder and create it if the object does not exist. You can use this construction with If/Else:
$path = "C:\MyAppFolder"
If(!(Test-Path -PathType container $path))
{
New-Item -ItemType Directory -Path $path
} 
Check validity of path with IsValid parameter
The IsValid parameter is used to check the validity of the path. This option checks the syntax of a specific path, regardless of whether the path exists:
Test-Path -Path "C:\MyFolder" -IsValid

How to Use LiteralPath with Special Characters
The Test-Path cmdlet supports wildcard characters such as `*` and `?` in the `-Path` parameter. In some cases, this may lead to incorrect results when the file/folder name contains special characters.
For example, below is the path that may be interpreted incorrectly because of square brackets:
Test-Path -Path "C:\Logs[2026].txt"
In this case, PowerShell treats [2026] as a wildcard pattern instead of a literal part of the file name.
In order to avoid this behavior, you should use the -LiteralPath parameter (which treats the path exactly as written without interpreting wildcard characters):
Test-Path -LiteralPath "C:\Logs[2026].txt"
Note that the -LiteralPath parameter is useful in case you are working with file names that contain special characters such as:
- Square brackets [ ]
- Asterisks *
- Question marks ?
This parameter ensures that the path is evaluated literally and not treated as a pattern.
NewerThan and OlderThan parameters
The NewerThan and OlderThan parameters can also be used with file system objects. They allow you to check if a file or folder’s timestamp is newer or older than a specified date. For example, check that the specified folder contains TXT files that has been modified within the last seven days:
Test-Path -Path "C:\LOGS\*.txt" -NewerThan (Get-Date).AddDays(-7)
Note. The behavior of -NewerThan and -OlderThan has changed across PowerShell versions. In particular, older PowerShell versions had limitations and bugs when these parameters were combined with -PathType or when you used both date parameters together. PowerShell 7.5 introduced improvements to date-range filtering and -PathType compatibility. If your scripts must run across different PowerShell versions, you should test these parameter combinations on the versions deployed in your environment.
You can use additional arguments to add or exclude certain types of objects from the check:
- Include
- Exclude
In order to use these parameters, you must specify an asterisk (*) at the end of the path, as shown in the example:
Test-Path -Include "*.log","*.tmp" -Path "C:\Windows\*"
Include specific object with Filter parameter
You can also use the Filter parameter to include specific object, but it only accepts one value:
Test-Path -Path "C:\Windows\*" -Filter "*.log"
Check if file or folder exist in shared network folder
The Test-Path command can be used to check the presence of objects in a shared network folder on a remote computer. Specify the path to the shared folder in UNC format:
Test-Path -Path "\\192.168.12.12\Logs\*" -Include "*.txt"
Note that Test-Path returns False not only when the path does not exist, but also when the current user cannot access the resource. For example, a network share may exist, but the command can still return False if the user lacks the required permissions/authentication to access it.
Here is an example:
Test-Path "\\server\restricted-share"
The output of the command:
False
In this case, you need to check if the network path is accessible and that your account has sufficient permissions to access the shared folder.
Alternatively, you can first map the shared folder as a network drive using the New-PSDrive cmdlet and then verify if the path exists:
New-PSDrive -Name M -PSProvider FileSystem -Root '\\192.168.12.12\Logs\' Test-Path "M:\*" -Include "*.txt"
You can also use the following syntax for network folder paths:
Test-Path $('filesystem::\\server1\share1\') 
Checking Multiple Paths with Test-Path
In PowerShell scripts, it is often necessary to check multiple files/folders at once. Instead of running `Test-Path` repeatedly, you can pass an array of paths. Follow one of the methods described below.
Method 1: Using an array
You can pass multiple paths directly as an array:
Test-Path @(
"C:\File1.txt",
"C:\File2.txt",
"C:\File3.txt"
)
This returns a separate Boolean result for each path.
Method 2: Using pipeline input
You can also check multiple paths using the pipeline:
$Paths = @(
"C:\File1.txt",
"C:\File2.txt",
"C:\File3.txt"
)
$Paths | Test-Path

Method 3: Filtering existing files
In order to return only existing files, you can combine results with Where-Object:
$Paths | Where-Object {
Test-Path -LiteralPath $_
} This can be useful when you need to process only valid paths in automation scripts.
Test registry paths, environment variables, and PowerShell aliases with Test-Path
The Test-Path cmdlet in PowerShell is not limited to files and folders. It can also be used to test registry paths, environment variables, and PowerShell aliases.
Check if the registry key exists:
Test-Path "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ActiveSync"
Check for the presence of an environment variable:
Test-Path env:computername
Check if a specific PowerShell alias is available:
Test-Path alias:wget
What is Test-Path in PowerShell?
Test-Path is a built-in cmdlet used to check whether a file, folder, registry key, environment variable, or other path-based object exists and is accessible. It returns True if the path exists, otherwise False.
What can Test-Path check?
It can validate the existence of:
- Files and folders
- Registry keys
- Environment variables
- PowerShell aliases
- Network shares (UNC paths)
What is the main limitation of Test-Path?
It only confirms:
- existence
- and accessibility
It does not validate:
- file content
- permissions beyond basic access failure detection
- correctness of file data structure
