I am making a Script that has to delete all the files in a directory that do not match the creation date with the year in which it is executed, that is, if I execute it in 2017 it deletes everything that has not been created that year.
I have managed to get the year, but I don't know how to delete the files:
$yearOs = Get-Date -Format yyyy
$yearItems = Get-ChildItem E:\Backup\ | get-date -Format yyyy
foreach ($i in $yearItems) {
if ($i -ne $yearOs) {
echo $i # esto da cualquier fecha que no sea el año en el que se ejecuta
}
}
I have also thought about using the command forfiles
but I already told you that in PS I am a little green.
This script will suffice
Explanation:
This line gets the child directories of
<ruta>
.This line makes a filter (where) and returns only directories that do not have the current year as modification date.
This line loops through each of the directories from the previous step and recursively removes them (including all their subdirectories).