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
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