Solved

need assistance with powershell script to delete items older than 20days

Posted on 2013-05-14
4
483 Views
Last Modified: 2013-05-14
can someone assist with this script. its working fine, but is not deleting the top level folders after it runs. [although it is deleting all of the data under the top level folders]

how can i modify to have the script also delete the top level folders. thx in advance.

# set folder path
$dump_path = "c:\data" 

# set min age of files
$max_days = "-20" 

# 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 $dump_path -Recurse | Where-Object { $_.LastWriteTime -lt $del_date } | Remove-Item -Recurse

Open in new window

0
Comment
Question by:siber1
  • 2
  • 2
4 Comments
 
LVL 39

Expert Comment

by:footech
ID: 39165691
I assume you mean the path specified by $dump_path.  Anything below that will be removed just fine with what you have (unless permissions are blocking you).  Of course, you'd only want to remove that path if there was nothing contained, otherwise there'd be no reason to perform the check in your script above.  So, you could just add the following to the end of your script.
If ( !(Get-ChildItem $dump_path -Recurse) )
{ Remove-Item $dump_path -Recurse }

Open in new window

0
 

Author Comment

by:siber1
ID: 39165726
Hi  footech.

just to clarify. in the c:\data [$dump_path]  we have a number of folders all sorted by date.
for example:
04-11-2013
04-12-2013
04-13-2013
...
05-14-2013

when i run the script above to purge out all data from this $dump_path, older than 20 days, i also want it to remove the top level folders older than 20 days.

so for example in the reference above. delete these folders:
04-11-2013
04-12-2013
04-13-2013

currently that script is removing all data in those older folders, but leaving the empty folders.

is that what your modification will do?

thx
0
 
LVL 39

Accepted Solution

by:
footech earned 500 total points
ID: 39165785
No.  Those folders should already be removed by what you have if they meet the criteria.  If you add the -whatif parameter to your Remove-Item command, you should see lines like the following for any folders that should be removed.
What if: Performing operation "Remove Directory" on Target "C:\data\04-11-2013".

Open in new window

Are you certain that the lastwritetime property of the folder is older than 20 days?
0
 

Author Closing Comment

by:siber1
ID: 39165798
thanks footech... that was it.
0

Join & Write a Comment

Disabling the Directory Sync Service Account in Office 365 will stop directory synchronization from working.
Scam emails are a huge burden for many businesses. Spotting one is not always easy. Follow our tips to identify if an email you receive is a scam.
In this video we show how to create an email address policy in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.:  First we need to log into the Exchange Admin Center. Navigate to the Mail Flow…
In this video we show how to create a mailbox database in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Servers >> Data…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now