Learn about Windows PowerShell
Summary: Microsoft Scripting Guy, Ed Wilson, discusses three ways to use Windows PowerShell to delete folders and then selects the best.
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
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.
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.
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.
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*
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.
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.
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.
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
Hi Ed,
there various ways to get rid of folders!
I usually use the rd alias and stay with PS native cmdlets this way.
Maybe we should mention the -force and -confirm parameters which maybe useful at times.
Additionally the -recurse parameter is an option tp delete a whole subtree of folders and files. You should ask PS, if you want to know more about the available parameters
"help rd -full" eg. is one option to do so ...
Klaus (Schulte)
@K_Schulte
I would not recommend using the -Recurse as if we check the documentation for Remove-Item we see Microsoft has a small note: "The Recurse parameter in this cmdlet does not work properly."
-Oscar Virot
@Oscar
OH! I didn't know that!
Up to now, it worked ... as far as i can tell ... for me!
But I will take your word of warning serious!
Thank you!
Klaus
How do i use this script on a schedule to delete files and folders older than a certain date?
Is there a way to delete files that are created or owned by a specific user??
How would you delete all subdirectories in a folder, except "some". I'm looking for a way to delete ghost profiles, except the ones for the Admins.
@chaleco86
At a command prompt type delprof /?