#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.

2. 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.

About the author: Ravikanth C

Ravikanth is the founder and editor of the PowerShell Magazine. He is also a PowerShell MVP who works at Dell Inc. He blogs at http://www.ravichaganti.com/blog and you can follow him on Twitter @ravikanth.

Related Posts