Move files older than a month PowerShell

0

Good morning, I have a question with a PowerShell script. It runs every 1 of every month, and you have to move to another route, all the files that were created before a month. That is, if you run on August 1 you have to move all files created until July 1, leaving untouched from July 1 to July 31.

I have this:

 $lastmonth = (get-date).AddMonths(-1).ToUniversalTime()

This gives me the same day but the month before the one that is executed, so if it is executed on day 1 it will give me the day 1 of the previous month.

I thought about making an "IF" fulfilling the condition that they were created before (-le) on the 1st of the previous month, but I am currently learning PowerShell and I am not very thin, I would appreciate any help.

So far I have this:

$lastmonth = (get-date).AddMonths(-1).ToUniversalTime()

Get-ChildItem E:\Backup |
    ? { $_.CreationTime -le $lastmonth  } |
    ForEach { Move-Item -path E:\Backup\* -destination F:\Backup_Process}

But I move all the files without discriminating, I have verified that until the ForEach the condition fulfills, showing the files before a month. Greetings

    
asked by genesis 08.08.2017 в 10:14
source

1 answer

0

Ok ... after a lot of trying I found the solution:

# Mover ficheros hasta un mes antes de su ejecución
#-------------------------------------------------------------------
$lastmonth = (get-date).AddMonths(-1).ToUniversalTime()

Get-ChildItem E:\Backup |

    ? { $_.CreationTime -le $lastmonth  } | 
        Move-Item -destination F:\Backup_Process
#-------------------------------------------------------------------

I myself was treading on the condition of the departure of the command that discriminated for months when trying to move everything. Here I leave it in case it is useful to someone. Greetings

    
answered by 08.08.2017 в 12:43