PowerShell Test-Path Tutorial

PowerShell provides the Test-Path cmdlet in order to check if the specified path exists. The Test-Path command returns boolean values like True and False. If the specified path exists the True value is returned. If the specified Path does not exist the False value is returned.

Test Path

The Test-Path command is used to check path existence. We can provide the path we want to check as string type. To prevent errors the best way to provide the path is using double quotes.

PS> Test-Path "C:\Users\ismail"
True

Test Path As Variable

PowerShell is a complete environment where variables can be used to store path information. We can use variables to test the path. In the following example, we check the PowerShell variable named $data which contains the path “D:\Data”.

PS> $data = "D:\Data"
PS> Test-Path $data
True

Test If File Exist

By default, the Test-Path command checks the specified path or directory existence. But we can also use the Test-Path in order to check file existence. The -PathType attribute set as leaf like below to check file existence.

PS> Test-Path -PathType leaf "D:\Data\names.txt"

Leave a Comment