PowerShell Logo Small

Test-Path



This is the built-in help made by Microsoft for the command 'Test-Path', in PowerShell version 5 - as retrieved from Windows version 'Microsoft Windows Server 2012 R2 Standard' PowerShell help files on 2016-06-23.

For PowerShell version 3 and up, where you have Update-Help, this command was run just before creating the web pages from the help files.

SYNOPSIS

Determines whether all elements of a path exist.

SYNTAX


Test-Path [-Path] <String[]> [-Credential [<PSCredential>]] [-Exclude [<String[]>]] [-Filter [<String>]] [-Include [<String[]>]] [-InformationAction {SilentlyContinue | Stop
| Continue | Inquire | Ignore | Suspend}] [-InformationVariable [<System.String>]] [-IsValid] [-NewerThan [<Nullable [System.DateTime]>]] [-OlderThan [<Nullable
[System.DateTime]>]] [-PathType {Any | Container | Leaf}] [-UseTransaction [<SwitchParameter>]] [<CommonParameters>]
Test-Path [-Credential [<PSCredential>]] [-Exclude [<String[]>]] [-Filter [<String>]] [-Include [<String[]>]] [-InformationAction {SilentlyContinue | Stop | Continue |
Inquire | Ignore | Suspend}] [-InformationVariable [<System.String>]] [-IsValid] [-NewerThan [<Nullable [System.DateTime]>]] [-OlderThan [<Nullable [System.DateTime]>]]
[-PathType {Any | Container | Leaf}] -LiteralPath <String[]> [-UseTransaction [<SwitchParameter>]] [<CommonParameters>]



Search powershellhelp.space

DESCRIPTION


The Test-Path cmdlet determines whether all elements of the path exist. It returns TRUE ($true) if all elements exist and FALSE ($false) if any are missing. It can also tell
whether the path syntax is valid and whether the path leads to a container or a terminal (leaf) element.



<

RELATED LINKS

Online Version: http://go.microsoft.com/fwlink/p/?linkid=293927
Convert-Path
Join-Path
Resolve-Path
Split-Path
about_Providers

REMARKS

<

Examples


-------------------------- EXAMPLE 1 --------------------------

PS C:\>test-path -path "C:\Documents and Settings\NicoleH"



This command tells whether all elements in the path exist, that is, the C: directory, the Documents and Settings directory, and the NicoleH directory. If any are missing,
the cmdlet returns FALSE. Otherwise, it returns TRUE.










-------------------------- EXAMPLE 2 --------------------------

PS C:\>test-path -path $profile
PS C:\>test-path -path $profile -IsValid



These commands test the path to the Windows PowerShell profile.

The first command determines whether all elements in the path exist. The second command determines whether the syntax of the path is correct. In this case, the path is
FALSE, but the syntax is correct (TRUE). These commands use $profile, the automatic variable that points to the location for the profile, even if the profile does not exist.

For more information about automatic variables, see about_Automatic_Variables.










-------------------------- EXAMPLE 3 --------------------------

PS C:\>test-path -path "C:\CAD\Commercial Buildings\*" -exclude *.dwg



This command tells whether there are any files in the Commercial Buildings directory other than .dwg files.

The command uses the Path parameter to specify the path. Because it includes a space, the path is enclosed in quotes. The asterisk at the end of the path indicates the
contents of the Commercial Building directory. (With long paths, like this one, type the first few letters of the path, and then use the TAB key to complete the path.)

The command uses the Exclude parameter to specify files that will be omitted from the evaluation.

In this case, because the directory contains only .dwg files, the result is FALSE.










-------------------------- EXAMPLE 4 --------------------------

PS C:\>test-path -path $profile -pathtype leaf



This command tells whether the path stored in the $profile variable leads to a file. In this case, because the Windows PowerShell profile is a .ps1 file, the cmdlet returns
TRUE.










-------------------------- EXAMPLE 5 --------------------------

PS C:\>test-path -path HKLM:\Software\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell
TRUE
PS C:\>test-path -path HKLM:\Software\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell\ExecutionPolicy
FALSE



These commands use the Test-Path cmdlet with the Windows PowerShell registry provider.

The first command tests whether the registry path to the Microsoft.PowerShell registry key is correct on the system. If Windows PowerShell is installed correctly, the cmdlet
returns TRUE.

Test-Path does not work correctly with all Windows PowerShell providers. For example, you can use Test-Path to test the path to a registry key, but if you use it to test the
path to a registry entry, it always returns FALSE, even if the registry entry is present.