Search notes:

PowerShell cmdLet Test-Path

test-path checks if all given paths exist and returns either $true or $false.
Because variables are stored in the drive variable:, test-path can also be used to check if a variable has been assigned a value.

Using negation of test-path in if statements

If test-path is used in an if statement to check if a file does not exist, the test-path must be enclosed in parentheses:
if (-not (test-path $txtFile)) {
   write-output "$txtFile does not exist"
}

Testing for directories/containers or files/items with -pathType

The parameter -pathType allows to make sure that the tested path not only exists but also if it is a file/item (-pathType leaf) or is a directory/container (-pathType container).
In the following script, the (absolute) path name of the script is assigned to the variable $scriptFile (see the automatic variable $myInvocation). Because (clearly), this path exists, test-path returns $true.
When the test for this file is executed with -pathType leaf, the cmdLet still returns $true because the script is a file.
However, when executed with -pathType container, the cmdLet returns $false because the script is not a directory.
Finally, when the directory that contains the script (split-path $scriptFile) is tested with -pathType container, it returns $true because it is a directory.
$scriptFile = $myInvocation.myCommand.path

test-path $scriptFile
#
#  True

test-path $scriptFile -pathType leaf
#
#  True

test-path $scriptFile -pathType container
#
#  False

test-path (split-path $scriptFile) -pathType container
#
#  True
Github repository about-PowerShell, path: /cmdlets/path/test/pathType.ps1

Verify the existence of functions, aliases etc.

test-path not only operates on file systems or registry keys, but also on other providers such as aliases or functions.
Check if mkdir and where are functions or aliases:
PS C:\Users\Rene> test-path function:\mkdir
True
PS C:\Users\Rene> test-path function:\where
False
PS C:\Users\Rene> test-path alias:\mkdir
False
PS C:\Users\Rene> test-path alias:\where
True

Create directory if it does not exist

The following example uses test-path to check if the directory $home\some-dir exists. If not, it uses new-item to create a directory:
if (! (test-path "$home\some-dir") ) {
   $null = new-item -itemType directory -path "$home\some-dir" 
   write-host "directory was created"
}
Github repository about-PowerShell, path: /cmdlets/path/test/create-directory-if-it-does-not-exist.ps1

Error message: Could not find a part of the path…

If the path being tested contains a question mark (?) and has at least 149 characters, test-path throws the error message Could not find a part of the path…, as for example in the following statement.
test-path 'A?123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567'
This error message is not thrown when one character is removed:
test-path 'A?12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456'
This stackoverflow question has answers with other possible reasons for this error message.

Alternative: System.IO.File/Directory::Exists()

An alternative to using test-path is to use the method Exists() of the .NET class System.IO.File or System.IO.Directory:
if ([System.IO.File]::Exists($profile)) {
   write-host "The profile file exists"
}
else {
   write-host "The profile file does not exist"
}
Github repository about-PowerShell, path: /cmdlets/path/test/IO.File.Exists.ps1

See also

The command parameter -credential.
Powershell command noun: path

Index