?
Solved

PowerShell

Posted on 2014-03-14
9
Medium Priority
?
249 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 35

Accepted Solution

by:
Dan Craciun earned 668 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 71

Assisted Solution

by:Qlemo
Qlemo earned 668 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 664 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
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
LVL 71

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 71

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 71

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
Loops Section Overview
Screencast - Getting to Know the Pipeline

850 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