#PSTip Validate if a folder exists

There are multiple ways to verify if a folder exists or not in PowerShell. The below two are my favorite ways of doing it.

  1. Using the System.IO.Directory .NET namespace

The Exists() method returns True if the item specified is a directory and exists. I often use this method in the ValidateScript of PowerShell advanced functions inPowerShell 2.0 and above.

  1. Using Test-Path cmdlet in PowerShell
Test-Path $foldername -PathType Container

The Test-Path cmdlet returns True if and only if the specified path is a directory and exists.

Share on: