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