Powershell - Test the presence of file or folder with Test-Path

To support me, you can subscribe to the channel, share and like the videos, disable your ad blocker or make a donation. Thank you!

Hello,

The -PathType argument to Test-Path allows us to check whether the item is a file or a folder

$folder = 'c:\windows'
$file = 'notepad.exe'
# Demonstration for a folder -PathType Container :
Test-Path -Path $folder -PathType Container
Test-Path -Path $folder -PathType Leaf
Test-Path -Path "$dossier\$fichier" -PathType any
Test-Path -Path "$folder$file" -PathType any
# Demonstration for a -PathType Leaf file :
Test-Path -Path "$folder$file" -PathType Container
Test-Path -Path "$folder$file" -PathType Leaf
Test-Path -Path "$dossier\$fichier" -PathType any
Test-Path -Path "$dossier\$fichier"

Related links