Test-Path

 

Additional Resources for Test-Path

 

Verifying the Existence of a File or Folder

http://www.microsoft.com/technet/scriptcenter/topics/msh/cmdlets/test-path.mspx

 

 

SYNOPSIS

Determines whether all elements of a path exist.

 

SYNTAX

Test-Path [-path] <string[]> [-pathType {<Any> | <Container> | <Leaf>}] [-isValid] [-include <string[]>] [-exclude <string[]>] [-filter <string>] [-credential <PSCredential>] [<CommonParameters>]

 

Test-Path [-literalPath] <string[]> [-pathType {<Any> | <Container> | <Leaf>}] [-isValid] [-include <string[]>] [-exclude <string[]>] [-filter <string>] [-credential <PSCredential>] [<CommonParameters>]

 

DETAILED 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.

 

PARAMETERS

 

-path <string[]>

Specifies a path to be tested. Wildcards are permitted. If the path includes spaces, enclose it in quotation marks. The parameter name ("-Path") is optional.

 

Required?

true

Position?

1

Default value

N/A - The path must be specified

Accept pipeline input?  

true (ByValue, ByPropertyName)

Accept wildcard characters? 

true

 

-pathType <TestPathType>

Determines the type of element that the path locates. Returns TRUE if the element is of the specified type and FALSE if it is not.

 

Valid values are:

 

·          Container: An element that contains other elements, such as a directory or registry key.

·          Leaf: An element that does not contain other elements, such as a file or registry entry.

·          Any: Either a container or a leaf.

 

Required?

false

Position?

named

Default value

Any

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

-isValid <SwitchParameter>

Determines whether the syntax of the path is correct, regardless of whether the elements of the path exist. Returns TRUE if the path syntax is valid; FALSE if it is not.

 

Required?

false

Position?

named

Default value

 

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

-include <string[]>

Tests only the specified paths. The value of this parameter qualifies the Path parameter. Enter a path element or pattern, such as "*.txt". Wildcards are permitted.

 

Required?

false

Position?

named

Default value

 

Accept pipeline input?  

false

Accept wildcard characters? 

true

 

-exclude <string[]>

Omits the specified items. The value of this parameter qualifies the Path parameter. Enter a path element or pattern, such as "*.txt". Wildcards are permitted.

 

Required?

false

Position?

named

Default value

 

Accept pipeline input?  

false

Accept wildcard characters? 

true

 

-filter <string>

Specifies a filter in the provider's format or language. The value of this parameter qualifies the Path parameter. The syntax of the filter, including the use of wildcards, depends on the provider. Filters are more efficient than other parameters, because the provider applies them when retrieving the objects, rather than having Windows PowerShell filter the objects after they are retrieved.

 

Required?

false

Position?

named

Default value

 

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

-credential <PSCredential>

Uses a credential to validate access to the resource. <Credential> represents a user-name, such as "User01" or "Domain01\User01", or a PSCredential object, such as the one retrieved by using the Get-Credential cmdlet. If you type a user name, you will be prompted for a password. This parameter appears, but it is not supported in any Windows PowerShell core cmdlets or providers.

 

Required?

false

Position?

named

Default value

 

Accept pipeline input?  

true (ByPropertyName)

Accept wildcard characters? 

false

 

-literalPath <string[]>

Specifies a path to be tested. Unlike Path, the value of LiteralPath is used exactly as it is typed. No characters are interpreted as wildcards. If the path includes escape characters, enclose it in single quotation marks. Single quotation marks tell Windows PowerShell not to interpret any characters as escape sequences.

 

Required?

true

Position?

1

Default value

 

Accept pipeline input?  

true (ByPropertyName)

Accept wildcard characters? 

false

 

<CommonParameters>

trace-command parameterbinding {get-alias $input} -pshost -inputobject $a 

INPUT TYPE

String

 

RETURN TYPE

Boolean

 

NOTES

 

For more information, type "Get-Help Test-Path -detailed". For technical information, type "Get-Help Test-Path -full".

 

The path cmdlets (*-path) manipulate path names and return the names in a concise format that all Windows PowerShell providers can interpret. They are designed for use in programs and scripts where you want to display all or part of a path name in a particular format. Use them like you would use Dirname, Normpath, Realpath, Join, or other path manipulators.

 

You can use the path cmdlets with several providers, including the FileSystem, Registry, and Certificate providers.

 

When specifying multiple values for a parameter, use commas to separate the values. For example, "<parameter-name> <value1>, <value2>".

 

EXAMPLE 1

 

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

 

This command determines whether all elements in the path exist.

 

EXAMPLE 2

 

test-path $PROFILE

 

test-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 built-in variable that points to the location for the profile even if none exists.

 

EXAMPLE 3

 

test-path C:\CAD\Commercial_buildings\* -exclude *.dwg

 

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

 

EXAMPLE 4

 

test-path $profile -pathtype leaf

 

This command determines whether the path stored in the $profile variable leads to a directory or a file.

 

RELATED LINKS

Convert-Path

Split-Path

Resolve-Path

Join-Path

about_namespace