We help IT Professionals succeed at work.

Erasing Folders & Files with PowerShell

Ray Turner
Ray Turner asked
on
356 Views
Last Modified: 2017-05-30
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

Senior Systems Engineer
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
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!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions