Solved

PowerShell

Posted on 2014-03-14
9
245 Views
Last Modified: 2014-03-20
How to write a powershell to remove files and subfolders older than 7 days in  a particular folder  ?

Tks
0
Comment
Question by:AXISHK
[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
9 Comments
 
LVL 35

Accepted Solution

by:
Dan Craciun earned 167 total points
ID: 39928630
$today=get-date
$oldDate = $today.addDays(-7)
gci "x:\your\path" | % {if ($_.CreationTime -lt $oldDate) {Remove-Item $_.FullName -Force}}

Open in new window

Replace x:\your\path with the actual path.

HTH,
Dan
0
 
LVL 70

Assisted Solution

by:Qlemo
Qlemo earned 167 total points
ID: 39928644
I would go with a two steps task. First delete all "old" files. Then go thru the folders and check if there are no files left, and if so delete the folder. This is because the folder timestamp isn't reliable for measuring age.
$date = (get-date).AddDays(-7)
dir c:\temp\root -recurse | ? { ! $_.PsIsContainer -and $_.LastWriteTime -lt $date } | remove-item

function recdel ($folder)
{
  dir $folder | ? { $_.PsIsContainer } | % { recdel $_ }
  if ( (dir $folder).Count -eq 0 ) { remove-item $folder }
}

recdel c:\temp\root

Open in new window

0
 
LVL 11

Assisted Solution

by:Joe Klimis
Joe Klimis earned 166 total points
ID: 39928653
I use this one liner

 Get-ChildItem -Recurse -Path "e:\temp" | ? { $_.LastWriteTime -lt (get-date).adddays(-
7)} | Remove-Item 

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 70

Expert Comment

by:Qlemo
ID: 39928822
That's a two-liner :-p

Assuming the folder time stamps are working, you both are obviously missing the point that you first need to delete subfolders before you can delete the parent ...
0
 
LVL 11

Expert Comment

by:Joe Klimis
ID: 39929350
You are right , it wrapped ;-)

I also tested this with a structure 3 folders deep and files in the lowest folder

This removed everything, in one go .   ( useing powershell V3 )
0
 

Author Comment

by:AXISHK
ID: 39936811
dir $folder | ? { $_.PsIsContainer } | % { recdel $_ }


What is the meaning for '?' and '%'  ?

Tks
0
 
LVL 11

Expert Comment

by:Joe Klimis
ID: 39936898
Hi

?  is a short cut for  where  statement
% is a shortcut for  ForEach statement

I hope that helps

Joe
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 39936913
Correction: Those are no statements but cmdlets. The difference is that you can't pipe into and out of a statement (by default). And the foreach statement is
foreach ($file in get-childitems c:\temp) { write-host $file }

Open in new window

while the foreach-object cmdlet (with a confusing alias of foreach and the popular alias %) is
get-childitems c:\temp | % { write-host $_ }

Open in new window

if, while are two examples of statements.
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 39936917
Correction: Those are no statements but cmdlets. The difference is that you can't pipe into and out of a statement (by default). And the foreach statement is
foreach ($file in get-childitems c:\temp) { write-host $file }

Open in new window

while the foreach-object cmdlet (with a confusing alias of foreach and the popular alias %) is
get-childitems c:\temp | % { write-host $_ }

Open in new window

if, while are two examples of statements.
0

Featured Post

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

689 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