![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Delete Files using powershell Hi all, How can we delete files in powershell, when the files to be deleted have a precondition like all the files which are say older then 3 days should be deleted only. Thanks, Aditya |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Delete Files using powershell On Aug 26, 6:30*am, AdityaKir <Aditya...@xxxxxx> wrote: Quote: > Hi all, > > How can we delete files in powershell, when the files to be deleted have a > precondition like all the files which are say older then 3 days should be > deleted only. > > Thanks, > > Aditya This will delete all the files in the current directory that are older than 3 days: Get-ChildItem | Where-Object {$_.LastWriteTime -gt (Get-Date).AddDays(-3)} | Remove-Item -WhatIf The Get-ChildItem part could be modified to be much more specific to what you want. You could make the command recursive or add filters for the types of files you are looking for. I recommend using the WhatIf parameter first, just to be sure it is doing what you want. Jeff |
My System Specs![]() |
| | #3 (permalink) |
| | RE: Delete Files using powershell PowerShell let's you define 'special' functions called Filters that you can pipe ojects to process, and output, the objects. Here are two examples filter files { if (!$_.psIsContainer) {$_} } filter daysOld ([single]$days = 0.0) { if ($_.lastWriteTime -gt (get-date).addDays(-$days)) {$_} } # now you can simply: C:\yourDir | files | daysOld 3 | remove-item -- Kiron |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Delete Files using powershell # oops! forgot the initial Cmdlet ls C:\yourDir | files | daysOld 3 | remove-item -- Kiron |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Delete Files using powershell Thanks Kiron and Jeff your solution guided me ![]() "Kiron" wrote: Quote: > # oops! forgot the initial Cmdlet > ls C:\yourDir | files | daysOld 3 | remove-item > > -- > Kiron > |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Looking to delete last 3 lines with powershell | PowerShell | |||
| How do I delete an OU in Active Directory from Powershell. | PowerShell | |||
| How to delete a DFS node using Powershell | PowerShell | |||
| Pecursive Delete Implementation in PowerShell | PowerShell | |||
| delete temp dlls via powershell | PowerShell | |||