[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2013-05-14
4
Medium Priority
?
494 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 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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

There can be many situations demanding the conversion of Outlook OST files to PST format and as such, there is no shortage of automated tools to perform this conversion. However, what makes Stellar OST to PST converter stand above the rest? Let us e…
Exchange administrators are always vigilant about Exchange crashes and disasters that are possible any time. It is quite essential to identify the symptoms of a possible Exchange issue and be prepared with a proper recovery plan. There are multiple…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

873 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