{"id":11071,"date":"2012-02-22T00:01:00","date_gmt":"2012-02-22T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/02\/22\/the-best-way-to-use-powershell-to-delete-folders\/"},"modified":"2012-02-22T00:01:00","modified_gmt":"2012-02-22T00:01:00","slug":"the-best-way-to-use-powershell-to-delete-folders","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/the-best-way-to-use-powershell-to-delete-folders\/","title":{"rendered":"The Best Way to Use PowerShell to Delete Folders"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, discusses three ways to use Windows PowerShell to delete folders and then selects the best.<\/p>\n<p><span><span><span><span><span><span><span><span><img decoding=\"async\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\" \/><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span>&nbsp;Hey, Scripting Guy! I have a question. I occasionally need to delete a large number of folders. What is the easiest way to do this?<\/p>\n<p>&mdash;BR<\/p>\n<\/p>\n<p><span><span><span><span><span><span><span><span><img decoding=\"async\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\" \/><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span>&nbsp;Hello BR,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. There are just as many ways to delete directories by using Windows PowerShell as there are ways to create new directories. Yesterday, I discussed <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/02\/21\/learn-four-ways-to-use-powershell-to-create-folders.aspx\" target=\"_blank\">four ways to create new folders by using Windows PowerShell<\/a>. Today I want to talk about deleting directories, and I will show you three ways to delete folders. Unlike yesterday, I want to talk about what I consider the best way to delete a directory first.<\/p>\n<h3>Method 1: Use native cmdlets<\/h3>\n<p>To delete folders, I like to use the <b>Remove-Item <\/b>cmdlet. There is an alias for the <b>Remove-Item<\/b> cmdlet called <b>rd<\/b><i>. <\/i>Unlike the <b>md<\/b><i> <\/i>function, <b>rd<\/b><i> <\/i>is simply an alias for <b>Remove-Item<\/b>. The following command reveals this information.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-Alias rd<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">CommandType&nbsp;&nbsp;&nbsp;&nbsp; Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Definition<\/p>\n<p style=\"padding-left: 30px\">&#8212;&#8212;&#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;-<\/p>\n<p style=\"padding-left: 30px\">Alias&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rd&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Remove-Item<\/p>\n<p>One of the main reasons I like to use the <b>Remove-Item<\/b> cmdlet to delete folders is that it implements the <i>WhatIf <\/i>switch. This means that I can run a command, such as deleting a bunch of folders, and see exactly which folders the command will remove. This technique is shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4331.hsg-2-22-12-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4331.hsg-2-22-12-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>After I examine the information that is returned by the <i>WhatIf <\/i>switch, I use the Up arrow to retrieve the command, and I then use the backspace to remove the <b>&ndash;whatif<\/b> portion of the command. After it is edited, I run the command, and no information returns from the <b>Remove-Item<\/b> cmdlet. This command is shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1817.hsg-2-22-12-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1817.hsg-2-22-12-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>OK, I deleted my test directories, so it is time to create some new ones. The following code creates four test directories off of the root.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; 1..4 | % {md &#8220;test$_&#8221;}<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Directory: C:\\<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Mode&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LastWriteTime&nbsp;&nbsp;&nbsp;&nbsp; Length Name<\/p>\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;&#8212;-&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212; &#8212;-<\/p>\n<p style=\"padding-left: 30px\">d&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2\/21\/2012&nbsp; 11:10 AM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; test1<\/p>\n<p style=\"padding-left: 30px\">d&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2\/21\/2012&nbsp; 11:10 AM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; test2<\/p>\n<p style=\"padding-left: 30px\">d&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2\/21\/2012&nbsp; 11:10 AM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; test3<\/p>\n<p style=\"padding-left: 30px\">d&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2\/21\/2012&nbsp; 11:10 AM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; test4<\/p>\n<p>To ensure that the test folders appear in the place I am expecting, I use the <b>dir<\/b><i> <\/i>command (alias for <b>Get-ChildItem<\/b>) as shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; dir c:\\test*<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Directory: C:\\<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Mode&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LastWriteTime&nbsp;&nbsp;&nbsp;&nbsp; Length Name<\/p>\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;&#8212;- &nbsp;&nbsp;&nbsp;&nbsp;&#8212;&#8212; &#8212;-<\/p>\n<p style=\"padding-left: 30px\">d&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2\/21\/2012&nbsp; 11:10 AM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; test1<\/p>\n<p style=\"padding-left: 30px\">d&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2\/21\/2012&nbsp; 11:10 AM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; test2<\/p>\n<p style=\"padding-left: 30px\">d&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2\/21\/2012&nbsp; 11:10 AM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; test3<\/p>\n<p style=\"padding-left: 30px\">d&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2\/21\/2012&nbsp; 11:10 AM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; test4<\/p>\n<h3>Method 2: FileSystemObject still works<\/h3>\n<p>Now, it is time to look at another method for deleting directories: the use of <b>FileSystemObject<\/b>.<i> <\/i>I first need to create an instance of <b>FileSystemObject<\/b><i>, <\/i>then I can use the <b>DeleteFolder<\/b><i> <\/i>method. These two commands are shown here.<\/p>\n<p style=\"padding-left: 30px\">$fso = New-Object -ComObject scripting.filesystemobject<\/p>\n<p style=\"padding-left: 30px\">$fso.DeleteFolder(&#8220;C:\\test*&#8221;)<\/p>\n<p>The use of the commands, in addition to the use of the <b>dir<\/b><i> <\/i>command to check the status of the four test folders are shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5432.hsg-2-22-12-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5432.hsg-2-22-12-03.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<h3>Method 3: Use .NET classes<\/h3>\n<p>The third way I want to illustrate uses the .NET Framework <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/62t64db3.aspx\" target=\"_blank\"><i>System.IO.Directory<\/i><\/a><i> <\/i>class to delete a folder. It is a bit more complicated. For one thing, it does not like wild cards in the path. An example of this is shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5773.hsg-2-22-12-04.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5773.hsg-2-22-12-04.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>The solution is to use Windows PowerShell to obtain the folders to delete, and then use the <b>ForEach-Object <\/b>cmdlet to call the method. The code to do this is shown here.<\/p>\n<p style=\"padding-left: 30px\">dir C:\\test* | foreach { [io.directory]::delete($_.fullname) }<\/p>\n<p>The use of the command and the associated output are shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4375.hsg-2-22-12-05.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4375.hsg-2-22-12-05.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>BR, that is all there is to using Windows PowerShell to delete folders. Join me tomorrow when I talk about more cool stuff.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, discusses three ways to use Windows PowerShell to delete folders and then selects the best. &nbsp;Hey, Scripting Guy! I have a question. I occasionally need to delete a large number of folders. What is the easiest way to do this? &mdash;BR &nbsp;Hello BR, Microsoft Scripting Guy, Ed Wilson, is [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[11,3,12,45],"class_list":["post-11071","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-folders","tag-scripting-guy","tag-storage","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, discusses three ways to use Windows PowerShell to delete folders and then selects the best. &nbsp;Hey, Scripting Guy! I have a question. I occasionally need to delete a large number of folders. What is the easiest way to do this? &mdash;BR &nbsp;Hello BR, Microsoft Scripting Guy, Ed Wilson, is [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/11071","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=11071"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/11071\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=11071"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=11071"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=11071"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}