Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Delete Files using powershell

381 views
Skip to first unread message

AdityaKir

unread,
Aug 25, 2008, 7:30:11 PM8/25/08
to
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

Jeff

unread,
Aug 25, 2008, 9:40:50 PM8/25/08
to
On Aug 26, 6:30 am, AdityaKir <Aditya...@discussions.microsoft.com>
wrote:

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

Kiron

unread,
Aug 25, 2008, 10:15:01 PM8/25/08
to
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

Kiron

unread,
Aug 26, 2008, 3:15:34 AM8/26/08
to
# oops! forgot the initial Cmdlet
ls C:\yourDir | files | daysOld 3 | remove-item

--
Kiron

AdityaKir

unread,
Aug 26, 2008, 6:05:02 PM8/26/08
to
Thanks Kiron and Jeff your solution guided me :)
0 new messages