PowerShell

How to write a powershell to remove files and subfolders older than 7 days in  a particular folder  ?

Tks
AXISHKAsked:
Who is Participating?
 
Dan CraciunConnect With a Mentor IT ConsultantCommented:
$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
 
QlemoConnect With a Mentor Batchelor, Developer and EE Topic AdvisorCommented:
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
 
Joe KlimisConnect With a Mentor Commented:
I use this one liner

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

Open in new window

0
Improved Protection from Phishing Attacks

WatchGuard DNSWatch reduces malware infections by detecting and blocking malicious DNS requests, improving your ability to protect employees from phishing attacks. Learn more about our newest service included in Total Security Suite today!

 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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
 
Joe KlimisCommented:
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
 
AXISHKAuthor Commented:
dir $folder | ? { $_.PsIsContainer } | % { recdel $_ }


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

Tks
0
 
Joe KlimisCommented:
Hi

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

I hope that helps

Joe
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.