Erasing Folders & Files with PowerShell

Ray Turner
Ray Turner used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Systems Engineer
Commented:
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
Ray TurnerSenior Consultant

Author

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
Ray TurnerSenior Consultant

Author

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 }
Ray TurnerSenior Consultant

Author

Commented:
Thanks!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial