19.8. Using Cmdlets to Work with Paths

PowerShell provides cmdlets designed to let you work with paths:

  • convert-path

  • join-path

  • resolve-path

  • split-path

  • test-path

NOTE

These cmdlets can be used with PowerShell providers other than the FileSystem provider. The following descriptions relate to their use with the FileSystem provider.

The test-path cmdlet allows you to test if all elements of a path exist. In addition to the common parameters, it supports the following parameters:

  • path — Specifies the path to be tested.

  • pathType — Specifies the type of element that the path locates. Permitted values are Container (a folder in the FileSystem provider), Leaf (a file in the FileSystem provider), and Any. The default value is Any.

  • include — Qualifies the value of the path parameter.

  • exclude — Qualifies the value of the path parameter.

  • isValid — Tests only whether the syntax of the path is valid. If present the existence of the path is not tested.

  • Filter — Specifies a filter to apply when retrieving objects.

  • Credential — Specifies a credential to get access to a resource.

The following command:

test-path "C:\Pro PowerShell\Chapter 19"

tests for the existence of the folder C:\Pro PowerShell\Chapter 19. In Figure 19-26, you can see that the folder exists and the command returns True.

At the time of running the following command:

test-path "C:\Pro PowerShell\Chapter 20"

the Chapter 20 folder had not been created, so the command returns False. Even though the folder doesn't exist, by using the isValid ...

Get Professional Windows® PowerShell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.