Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Delete Files using powershell

Reply
 
Old 08-25-2008   #1 (permalink)
AdityaKir


 
 

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 SpecsSystem Spec
Old 08-25-2008   #2 (permalink)
Jeff


 
 

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
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 SpecsSystem Spec
Old 08-25-2008   #3 (permalink)
Kiron


 
 

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 SpecsSystem Spec
Old 08-26-2008   #4 (permalink)
Kiron


 
 

Re: Delete Files using powershell

# oops! forgot the initial Cmdlet
ls C:\yourDir | files | daysOld 3 | remove-item

--
Kiron
My System SpecsSystem Spec
Old 08-26-2008   #5 (permalink)
AdityaKir


 
 

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 SpecsSystem Spec
Reply

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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46