Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2013-05-14
4
Medium Priority
?
493 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 41

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 41

Accepted Solution

by:
footech earned 2000 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

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A walk-through example of how to obtain and apply new DID phone numbers to your cloud PBX enabled users that are configured in Office 365. Whether you have 1, 10 or 100+ users in your tenant, it's quite easy to get them phone-enabled and making/rece…
The main intent of this article is to make you aware of ‘Exchange fail to mount’ error, its effects, causes, and solution.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video discusses moving either the default database or any database to a new volume.
Suggested Courses

636 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