0

#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

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

Filed in: Columns, Tips and Tricks Tags: , ,

Leave a Reply

Submit Comment

© 2016 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: