Solved

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

Posted on 2013-05-14
4
486 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 40

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 40

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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A list of top three free exchange EDB viewers that helps the user to extract a mailbox from an unmounted .edb file and get a clear preview of all emails & other items with just a single click on mailboxes.
This article describes my battle tested process for setting up delegation. I use this process anywhere that I need to setup delegation. In the article I will show how it applies to Active Directory
To show how to generate a certificate request 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 >> Certificates…
In this Micro Video tutorial you will learn the basics about Database Availability Groups and How to configure one using a live Exchange Server Environment. The video tutorial explains the basics of the Exchange server Database Availability grou…

685 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