namerg
asked on
How to maintain files for 14months /426 days ?
I have been asked to keep files for about 14 month from the following directory d:\logs.
will the following code do that ? Or will just delete files older than 14 months ?
Thanks for your help
will the following code do that ? Or will just delete files older than 14 months ?
$limit = (Get-Date).AddDays(-426)
$path = "D:\Logs"
# Delete files older than the $limit.
Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force
Thanks for your help
ASKER
This is the code, but i am kind of stuck trying to understand the logic of keeping the file for 14months...so i guess that means that the following file
"D:\Logs\DocService\ArchivedRequests\2015\11\554080_483d18fef5
6a4637bd7622ce38bb5793-2"
is older than 14monts and ok to delete, right ?PS X:\> Get-ChildItem -Path $path -File -Recurse -Force | Where-Object {$_.LastWriteTime -lt $limit} | Remove-Item -Forc
e -WhatIf
What if: Performing the operation "Remove File" on target "D:\Logs\DocService\ArchivedRequests\2015\11\554080_483d18fef5
6a4637bd7622ce38bb5793-2".
What if: Performing the operation "Remove File" on target "D:\Logs\DocService\ArchivedRequests\2015\11\554080_a31822b503
fd4cbaa2b8169cb659deab-1".
What if: Performing the operation "Remove File" on target "D:\Logs\WFRouterService\2013\April\03-31to04-06\01\382139-MER
S".
What if: Performing the operation "Remove File" on target "D:\Logs\WFRouterService\2013\April\03-31to04-06\01\382383-MER
S".
What if: Performing the operation "Remove File" on target "D:\Logs\WFRouterService\2013\April\03-31to04-06\01\383118-MER
S".
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
So, the script will be
Right ?
$limit = (Get-Date).AddDays(-426)
$path = "D:\Logs"
# Delete files older than the $limit.
Get-ChildItem -Path $path -File -Recurse -Force |
Where-Object {$_.LastWriteTime -lt $limit} |
ForEach-Object {
"[$($_.LastWriteTime)] $($_.FullName)"
Remove-Item -Path $_.FullName -Force -WhatIf
}
Right ?
Yes. It's still completely harmless, because -WhatIf is still passed to Remove-Item.
ASKER
Thank you sir
And you'll want to use -File instead of filtering after retrieval (PS 3.0 or later assumed).
And you can just add -WhatIf to Remove-Item to see what it would display, without actually deleting anything.
Open in new window