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
).
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
Verify the existence of functions, aliases etc.
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
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'