Erasing Folders & Files with PowerShell

How do you erase a specified folder and its sub-folders using PowerShell?

I want to automatically erase ALL files within each sub-folder.

Thanks
Ray TurnerSenior ConsultantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Elango SathyadevSenior Systems EngineerCommented:
Command is as follows

Remove-item <Folder Path> -Recurse

Open in new window


Eg, If i want to remove a folder called test inside my directory e:\temp

here is teh command

Remove-item e:\temp\test -Recurse

This will remove Folder Test along with all files and all subfolders

Some additional command for you KB: https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.management/remove-item?f=255&MSPPError=-2147217396

https://ss64.com/ps/remove-item.html
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray TurnerSenior ConsultantAuthor Commented:
That does not work.  Here's the code I'm trying to run

$targetFiles = Get-ChildItem -Path l:\sync -Recurse
   
foreach ($targetFile in $targetFiles)
{
    $targetFile.FullName
}

Remove-item l:\sync -Recurse

Here's the output

PS L:\> L:\Write to WD.ps1
L:\sync\VBApps
L:\sync\VBApps\DLLs
L:\sync\VBApps\DLLs\GinesthoiSystems.Win
L:\sync\VBApps\DLLs\GinesthoiSystems.Win\GinesthoiSystems.Win
L:\sync\VBApps\DLLs\GinesthoiSystems.Win\GinesthoiSystems.Win\bin
L:\sync\VBApps\DLLs\GinesthoiSystems.Win\GinesthoiSystems.Win\bin\Debug
L:\sync\VBApps\DLLs\GinesthoiSystems.Win\GinesthoiSystems.Win\bin\Debug\DevExpress.XtraEditors.v16.2.xml
Remove-item : Directory L:\sync\VBApps\DLLs\GinesthoiSystems.Win\GinesthoiSystems.Win\bin\Debug cannot be removed because it is not
empty.
At L:\Write to WD.ps1:20 char:1
+ Remove-item l:\sync -Recurse
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (Debug:DirectoryInfo) [Remove-Item], IOException
    + FullyQualifiedErrorId : DirectoryNotEmpty,Microsoft.PowerShell.Commands.RemoveItemCommand
 
Remove-item : Directory L:\sync\VBApps\DLLs\GinesthoiSystems.Win\GinesthoiSystems.Win\bin cannot be removed because it is not empty.
At L:\Write to WD.ps1:20 char:1
+ Remove-item l:\sync -Recurse
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (bin:DirectoryInfo) [Remove-Item], IOException
    + FullyQualifiedErrorId : DirectoryNotEmpty,Microsoft.PowerShell.Commands.RemoveItemCommand
 
Remove-item : Directory L:\sync\VBApps\DLLs\GinesthoiSystems.Win\GinesthoiSystems.Win cannot be removed because it is not empty.
At L:\Write to WD.ps1:20 char:1
+ Remove-item l:\sync -Recurse
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (GinesthoiSystems.Win:DirectoryInfo) [Remove-Item], IOException
    + FullyQualifiedErrorId : DirectoryNotEmpty,Microsoft.PowerShell.Commands.RemoveItemCommand
 
Remove-item : Directory L:\sync\VBApps\DLLs\GinesthoiSystems.Win cannot be removed because it is not empty.
At L:\Write to WD.ps1:20 char:1
+ Remove-item l:\sync -Recurse
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (GinesthoiSystems.Win:DirectoryInfo) [Remove-Item], IOException
    + FullyQualifiedErrorId : DirectoryNotEmpty,Microsoft.PowerShell.Commands.RemoveItemCommand
 
Remove-item : Directory L:\sync\VBApps\DLLs cannot be removed because it is not empty.
At L:\Write to WD.ps1:20 char:1
+ Remove-item l:\sync -Recurse
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (DLLs:DirectoryInfo) [Remove-Item], IOException
    + FullyQualifiedErrorId : DirectoryNotEmpty,Microsoft.PowerShell.Commands.RemoveItemCommand
 
Remove-item : Directory L:\sync\VBApps cannot be removed because it is not empty.
At L:\Write to WD.ps1:20 char:1
+ Remove-item l:\sync -Recurse
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (VBApps:DirectoryInfo) [Remove-Item], IOException
    + FullyQualifiedErrorId : DirectoryNotEmpty,Microsoft.PowerShell.Commands.RemoveItemCommand
 
Remove-item : Directory L:\sync cannot be removed because it is not empty.
At L:\Write to WD.ps1:20 char:1
+ Remove-item l:\sync -Recurse
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (L:\sync:DirectoryInfo) [Remove-Item], IOException
    + FullyQualifiedErrorId : DirectoryNotEmpty,Microsoft.PowerShell.Commands.RemoveItemCommand
0
Ray TurnerSenior ConsultantAuthor Commented:
I tried the following and it did not work.  How do I fix this?

Get-ChildItem l:\Sync -Include *.* -recurse | foreach ($_) { remove-item $_.fullname }
0
Ray TurnerSenior ConsultantAuthor Commented:
Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.