SOLVED

Delete folder x

Copper 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 (Copper 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 

 

1 best response

Accepted Solutions
best response confirmed by mahipundir (Copper 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 

 

View solution in original post