Friday, May 24, 2013

PowerShell : Validate if a folder exists


There are multiple ways to verify if a folder exists or not in PowerShell. 
1. Using the System.IO.Directory .NET namespace
[System.IO.Directory]::Exists($foldername)
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.