Solved

PowerShell

Posted on 2014-03-14
9
243 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 69

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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 69

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 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 69

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 69

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

856 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