The Best Way to Use PowerShell to Delete Folders

Summary: Microsoft Scripting Guy, Ed Wilson, discusses three ways to use Windows PowerShell to delete folders and then selects the best.

Hey, Scripting Guy! Question 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?

—BR

Hey, Scripting Guy! Answer Hello BR,

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 four ways to create new folders by using Windows PowerShell. 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.

Method 1: Use native cmdlets

To delete folders, I like to use the Remove-Item cmdlet. There is an alias for the Remove-Item cmdlet called rd. Unlike the md function, rd is simply an alias for Remove-Item. The following command reveals this information.

PS C:\> Get-Alias rd

 

CommandType     Name                               Definition

———–     —-                               ———-

Alias           rd                                 Remove-Item

One of the main reasons I like to use the Remove-Item cmdlet to delete folders is that it implements the WhatIf 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.

Image of command output

After I examine the information that is returned by the WhatIf switch, I use the Up arrow to retrieve the command, and I then use the backspace to remove the –whatif portion of the command. After it is edited, I run the command, and no information returns from the Remove-Item cmdlet. This command is shown here.

Image of command output

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.

PS C:\> 1..4 | % {md “test$_”}

  

    Directory: C:\

 

Mode                LastWriteTime     Length Name

—-                ————-     —— —-

d—-         2/21/2012  11:10 AM            test1

d—-         2/21/2012  11:10 AM            test2

d—-         2/21/2012  11:10 AM            test3

d—-         2/21/2012  11:10 AM            test4

To ensure that the test folders appear in the place I am expecting, I use the dir command (alias for Get-ChildItem) as shown here.

PS C:\> dir c:\test*

 

    Directory: C:\

 

Mode                LastWriteTime     Length Name

—-                ————-     —— —-

d—-         2/21/2012  11:10 AM            test1

d—-         2/21/2012  11:10 AM            test2

d—-         2/21/2012  11:10 AM            test3

d—-         2/21/2012  11:10 AM            test4

Method 2: FileSystemObject still works

Now, it is time to look at another method for deleting directories: the use of FileSystemObject. I first need to create an instance of FileSystemObject, then I can use the DeleteFolder method. These two commands are shown here.

$fso = New-Object -ComObject scripting.filesystemobject

$fso.DeleteFolder(“C:\test*”)

The use of the commands, in addition to the use of the dir command to check the status of the four test folders are shown in the image that follows.

Image of command output

Method 3: Use .NET classes

The third way I want to illustrate uses the .NET Framework System.IO.Directory 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.

Image of command output

The solution is to use Windows PowerShell to obtain the folders to delete, and then use the ForEach-Object cmdlet to call the method. The code to do this is shown here.

dir C:\test* | foreach { [io.directory]::delete($_.fullname) }

The use of the command and the associated output are shown in the image that follows.

Image of command output

BR, that is all there is to using Windows PowerShell to delete folders. Join me tomorrow when I talk about more cool stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy