Solved

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

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

Featured Post

Don't lose your head updating email signatures!

Do your end users still have the wrong email signature? Do email signature updates bore you or fill you with a sense of dread? You can make this a whole lot easier on yourself by trusting an Exclaimer email signature management solution. Over 50 million users do...so should you!

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video we show how to create a Contact 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 Recipients >> Contact ta…
In this video we show how to create an Address List 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 Organization >> Ad…

863 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

24 Experts available now in Live!

Get 1:1 Help Now