Solved

PowerShell

Posted on 2014-03-14
9
236 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
9 Comments
 
LVL 34

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 68

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 10

Assisted Solution

by:JoeKlimis
JoeKlimis 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
 
LVL 68

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 10

Expert Comment

by:JoeKlimis
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 10

Expert Comment

by:JoeKlimis
ID: 39936898
Hi

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

I hope that helps

Joe
0
 
LVL 68

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 68

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

21 Experts available now in Live!

Get 1:1 Help Now