Answered by:
Powershell Remove files

Question
-
Good evening.
I have a question about Powershell script.
I want to create a script to delete files after 3 days but I don't want to delete files of the first day, 15th and 30 or 31 from each month. I have this:
Get-Childitem B:\TEST\*.txt | Where-Object { $_.LastWriteTime –lt (get-date).AddDays(-3) }| Remove-Item -Force
Greetings,.
Carlos Márquez
San Pedro Sula
Honduras- Edited by Carlos Márquez Friday, December 22, 2017 8:38 PM
Friday, December 22, 2017 8:37 PM
Answers
-
Finally, I have this:
Get-ChildItem -Path "B:\TEST" | Where-Object { ($_.LastWriteTime.Day -eq ([datetime]::Today.Day - 4)) -and ($_.LastWriteTime.Month -eq [datetime]::Today.Month -or $_.LastWriteTime.Month -eq [datetime]::Today.Month -1) -and ($_.LastWriteTime.Day -notin(1,15,[datetime]::DaysInMonth($_.LastAccessTime.Year,$_.LastAccessTime.Month))) -and $_.Extension -eq '.rar' } |Remove-Item -Force
Carlos Márquez
San Pedro Sula
Honduras- Marked as answer by Carlos Márquez Thursday, December 28, 2017 10:50 PM
Thursday, December 28, 2017 10:49 PM
All replies
-
perform a foreach on all file returned, determine the age in days, and apply a switch statement?Friday, December 22, 2017 9:00 PM
-
Where-Object{([datetime]::Today - $_.LastWriteTime).Days -gt 3 -and $_.LastWriteTime.Day -notin (1, 15, 30,31)}
\_(ツ)_/
- Proposed as answer by BOfH-666 Saturday, December 23, 2017 4:07 AM
Friday, December 22, 2017 10:55 PM -
Hi,
Just checking in to see if the information provided was helpful. Please let us know if you would like further assistance.
Best Regards,
FrankPlease remember to mark the replies as answers if they help and unmark them if they provide no help.
If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.Monday, December 25, 2017 8:46 AM -
Hi, JRV.
Thanks for help me.
I only have an observation in your answer, when I said 30 or 31 I'm talking about that I don't want to delete files from the last day of the month.
Greetings,
Carlos Márquez
San Pedro Sula
HondurasTuesday, December 26, 2017 1:54 PM -
Then you need to test each file date independently for last day.
$_.LastWriteTime.Month -eq $_.LastWriteTime.AddDays(1).Month
\_(ツ)_/
- Edited by jrv Tuesday, December 26, 2017 2:03 PM
Tuesday, December 26, 2017 2:03 PM -
Another more complicated method is:
$_.LastWriteTime -ne [datetime]::DaysInMonth($_.LastWriteTime.Year,$_.LastWriteTime.Month)
Note that the last day can be 28,29,30,31. Don't forget February and leap years.
\_(ツ)_/
- Edited by jrv Tuesday, December 26, 2017 2:09 PM
Tuesday, December 26, 2017 2:09 PM -
I think is something like that:
Where-Object{([datetime]::Today - $_.LastWriteTime).Days -gt 3 -and $_.LastWriteTime.Day -notin (1, 15, [datetime]::DaysInMonth($_.LastWriteTime.Year,$_.LastWriteTime.Month))}
Can work in this way?
Greetings,
Carlos Márquez
San Pedro Sula
HondurasTuesday, December 26, 2017 2:23 PM -
Finally, I have this:
Get-ChildItem -Path "B:\TEST" | Where-Object { ($_.LastWriteTime.Day -eq ([datetime]::Today.Day - 4)) -and ($_.LastWriteTime.Month -eq [datetime]::Today.Month -or $_.LastWriteTime.Month -eq [datetime]::Today.Month -1) -and ($_.LastWriteTime.Day -notin(1,15,[datetime]::DaysInMonth($_.LastAccessTime.Year,$_.LastAccessTime.Month))) -and $_.Extension -eq '.rar' } |Remove-Item -Force
Carlos Márquez
San Pedro Sula
Honduras- Marked as answer by Carlos Márquez Thursday, December 28, 2017 10:50 PM
Thursday, December 28, 2017 10:49 PM