Quantcast
Channel: PowerShell General
Viewing all articles
Browse latest Browse all 10624

Remove-Item in folder\sub-folders

$
0
0

Hi,

I wrote this script, delete files from folder and sub folders that older then X days:

# set folder path

$path = "C:\Temp\"

# set min age of files

$max_days = "-60"

# get the current date

$curr_date = Get-Date

# determine how far back we go based on current date

$del_date = $curr_date.AddDays($max_days)

# delete the files

Get-ChildItem $path -Recurse | Where-Object { $_.LastWriteTime -lt $del_date } | Remove-Item

my problem is that if there is an empty sub-folder he delete this folder also, how can I change the script that he will not delete empty folders\sub-folders?

 

Thanks,

Lior

 


Viewing all articles
Browse latest Browse all 10624