Powershell script to delete files older than x hours

rimmena
rimmena used Ask the Experts™
on
I have a script that can move files based on x hours to another directory. I want to adjust it so that instead of moving the files, it deletes them.
dir "e:\test" *.log |select name, fullname, Path, LastWriteTime , @{Expression={((get-date) - $_.CreationTime).TotalHours};Name="Hours"} | Where {$_.Hours -ge 72} | ForEach{$m="e:\test\archive" + $_.Name;move $_.FullName $m}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

dir "c:\dev\deletePowershell\test" *.log |select name, fullname, Path, LastWriteTime , @{Expression={((get-date) - $_.CreationTime).TotalHours};Name="Hours"} | Where {$_.Hours -ge 72} | ForEach{$m="e:\test\archive" + $_.Name;remove-item  $_.FullName}

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial