Avatar of RayT
RayT
Flag for United States of America asked on

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
Powershell

Avatar of undefined
Last Comment
RayT

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Elango Sathyadev

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
RayT

ASKER
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
RayT

ASKER
I tried the following and it did not work.  How do I fix this?

Get-ChildItem l:\Sync -Include *.* -recurse | foreach ($_) { remove-item $_.fullname }
RayT

ASKER
Thanks!
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck