{"id":256,"date":"2021-03-13T06:51:28","date_gmt":"2021-03-13T14:51:28","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/powershell-community\/?p=256"},"modified":"2021-03-13T06:53:27","modified_gmt":"2021-03-13T14:53:27","slug":"determine-if-a-folder-exists","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell-community\/determine-if-a-folder-exists\/","title":{"rendered":"Determine if a folder exists"},"content":{"rendered":"<p><strong>Q:<\/strong> Is there any way to determine whether or not a specific folder exists on a computer? <strong>A:<\/strong> There are loads of ways you can do this.<\/p>\n<h2>The Test-Path Cmdlet<\/h2>\n<p>The easiest way to do this is to use the <code>Test-Path<\/code> cmdlet. It looks for a given path and returns <code>True<\/code> if it exists, otherwise it returns <code>False<\/code>. You could evaluate the result of the <code>Test-Path<\/code> like in the code snippet below<\/p>\n<pre><code class=\"powershell\">$Folder = 'C:\\Windows'\n\"Test to see if folder [$Folder]  exists\"\nif (Test-Path -Path $Folder) {\n    \"Path exists!\"\n} else {\n    \"Path doesn't exist.\"\n}\n<\/code><\/pre>\n<p>This is similar to the <code>-d $filepath<\/code> operator for IF statements in Bash. <code>True<\/code> is returned if <code>$filepath<\/code> exists, otherwise <code>False<\/code> is returned.<\/p>\n<h2>For More Information<\/h2>\n<p>And for more information on <code>Test-Path<\/code> see the <a href=\"https:\/\/docs.microsoft.com\/powershell\/module\/microsoft.powershell.management\/test-path\">Test-Path<\/a> help page.<\/p>\n<h2>Summary<\/h2>\n<p>So as you saw, <code>Test-Path<\/code> tests the existence of a path and returns a boolean value. This return value can be evaluated in a IF statement for example.<\/p>\n<h2>Tip of the Hat<\/h2>\n<p>This article is based on an earlier Scripting Guys blog article at <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-determine-if-a-folder-exists-on-a-computer\/\">How can I determine if a folder exists on a computer?<\/a>. I am not sure who wrote the original article.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Q: Is there any way to determine whether or not a specific folder exists on a computer? A: There are loads of ways you can do this. The Test-Path Cmdlet The easiest way to do this is to use the Test-Path cmdlet. It looks for a given path and returns True if it exists, otherwise [&hellip;]<\/p>\n","protected":false},"author":55325,"featured_media":77,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[13],"tags":[15,8,16],"class_list":["post-256","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-file","tag-scripting-guys-update","tag-test-path"],"acf":[],"blog_post_summary":"<p>Q: Is there any way to determine whether or not a specific folder exists on a computer? A: There are loads of ways you can do this. The Test-Path Cmdlet The easiest way to do this is to use the Test-Path cmdlet. It looks for a given path and returns True if it exists, otherwise [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/posts\/256","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/users\/55325"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/comments?post=256"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/posts\/256\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/media\/77"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/media?parent=256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/categories?post=256"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/tags?post=256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}