Link to home
Start Free TrialLog in
Avatar of RayT
RayTFlag 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
ASKER CERTIFIED SOLUTION
Avatar of Elango Sathyadev
Elango Sathyadev
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 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
Avatar of 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 }
Avatar of RayT

ASKER

Thanks!