SOLVED

Delete folder x

%3CLINGO-SUB%20id%3D%22lingo-sub-936604%22%20slang%3D%22en-US%22%3EDelete%20folder%20x%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-936604%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20fellow%20humans%2C%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20using%20this%20script%20to%20delete%20folders%20which%20are%20older%20than%2015days%20or%205%20days.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3E%23%0A%24limit%20%3D%20(Get-Date).AddDays(-15)%0A%24path%20%3D%20%22xxxx%5Cpath%22%0A%0A%23%20Delete%20files%20older%20than%20the%20%24limit.%0AGet-ChildItem%20-Path%20%24path%20-Recurse%20-Force%20%7C%20Where-Object%20%7B%20!%24_.PSIsContainer%20-and%20%24_.CreationTime%20-lt%20%24limit%20%7D%20%7C%20Remove-Item%20-Force%0A%0A%23%20Delete%20any%20empty%20directories%20left%20behind%20after%20deleting%20the%20old%20files.%0AGet-ChildItem%20-Path%20%24path%20-Recurse%20-Force%20%7C%20Where-Object%20%7B%20%24_.PSIsContainer%20-and%20(Get-ChildItem%20-Path%20%24_.FullName%20-Recurse%20-Force%20%7C%20Where-Object%20%7B%20!%24_.PSIsContainer%20%7D)%20-eq%20%24null%20%7D%20%7C%20Remove-Item%20-Force%20-Recurse%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3BBut%20it%20doesn't%20work%20always%2C%20and%20I%20couldn't%20figure%20out%20what's%20wrong.%20is%20there%20any%20other%20way%20I%20can%20delete%20certain%20folders%20for%20example%20if%20there%20are%20more%20then%205%20folders..%3F%20or%20is%20it%20best%20to%20delete%20which%20one%20is%20older..%3F%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20do%20a%20backup%20via%20PS%20script%20which%20works%20great%2C%20but%20when%20I'm%20using%20this%20script%20to%20delete%20older%20backups%20some%20reason%20it%20doesn't%20work%20always.%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eis%20there%20any%20alternative%20way%20to%20do%20this.%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EKind%20Regards%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-936604%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1118062%22%20slang%3D%22en-US%22%3ERe%3A%20Delete%20folder%20x%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1118062%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F396090%22%20target%3D%22_blank%22%3E%40mahipundir%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3EGet-ChildItem%20-path%20%24path%20-Recurse%20-Force%20-Directory%3C%2FPRE%3E%3CP%3Ewill%20create%20a%20list%20of%20(sub)%20directories%2C%20and%20then%20you%20can%20filter%20for%20empty%20directories%20by%20checking%20for%20members%20of%20this%20list%20with%20no%20child%20items.%3C%2FP%3E%3CPRE%3EWhere-Object%20%7B!(%24_%20%7CGet-ChildItem%20-Recurse)%7D%3C%2FPRE%3E%3CP%3ESo%20put%20together%20that%20would%20give%3A%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CPRE%3EGet-ChildItem%20-path%20%24Path%20-Recurse%20-Force%20-Directory%20%7C%20Where-Object%20%7B!(%24_%20%7CGet-ChildItem%20-Recurse)%7D%3C%2FPRE%3E%3CP%3EHowever%2C%20if%20a%20folder%20contains%20no%20files%2C%20but%20does%20contain%20(empty)%20subfolders%20then%20it%20won't%20appear%20in%20this%20list%2C%20so%20assuming%20you%20want%20to%20delete%20folders%20which%20might%20contain%20subfolders%20but%20don't%20have%20any%20files%20anywhere%20down%20the%20tree%20then%20you%20need%20something%20like%3A%3C%2FP%3E%3CPRE%3EGet-ChildItem%20-path%20%24Path%20-Recurse%20-Force%20-Directory%20%7C%20Where-Object%20%7B!(%24_%20%7CGet-ChildItem%20-Recurse%20-File)%7D%20%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAnd%20then%20add%20the%20actual%20deletion%20as%20before%3A%3C%2FP%3E%3CPRE%3EGet-ChildItem%20-path%20%24Path%20-Recurse%20-Force%20-Directory%20%7C%20Where-Object%20%7B!(%24_%20%7CGet-ChildItem%20-Recurse%20-File)%7D%20%7C%20Remove-Item%20-Force%20-Recurse%20%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

Hello fellow humans, 

I'm using this script to delete folders which are older than 15days or 5 days. 

 

#
$limit = (Get-Date).AddDays(-15)
$path = "xxxx\path"

# Delete files older than the $limit.
Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force

# Delete any empty directories left behind after deleting the old files.
Get-ChildItem -Path $path -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null } | Remove-Item -Force -Recurse              	         

 

 But it doesn't work always, and I couldn't figure out what's wrong. is there any other way I can delete certain folders for example if there are more then 5 folders..? or is it best to delete which one is older..? 

 

I do a backup via PS script which works great, but when I'm using this script to delete older backups some reason it doesn't work always.  

 

is there any alternative way to do this.?

 

Kind Regards

1 Reply
best response confirmed by mahipundir (Occasional Contributor)
Solution

@mahipundir 

Get-ChildItem -path $path -Recurse -Force -Directory

will create a list of (sub) directories, and then you can filter for empty directories by checking for members of this list with no child items.

Where-Object {!($_ |Get-ChildItem -Recurse)}

So put together that would give:

Get-ChildItem -path $Path -Recurse -Force -Directory | Where-Object {!($_ |Get-ChildItem -Recurse)}

However, if a folder contains no files, but does contain (empty) subfolders then it won't appear in this list, so assuming you want to delete folders which might contain subfolders but don't have any files anywhere down the tree then you need something like:

Get-ChildItem -path $Path -Recurse -Force -Directory | Where-Object {!($_ |Get-ChildItem -Recurse -File)} 

 

And then add the actual deletion as before:

Get-ChildItem -path $Path -Recurse -Force -Directory | Where-Object {!($_ |Get-ChildItem -Recurse -File)} | Remove-Item -Force -Recurse