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 ?

$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

Open in new window


Thanks for your help
namergSystems AdministratorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
Don't use CreationTime - this is the time the file was created on the drive. When the file was copied, this can be later than LastWriteTime.
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.
$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} | Remove-Item -Force -WhatIf

Open in new window

0
namergSystems AdministratorAuthor Commented:
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"

Open in new window

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".

Open in new window

0
oBdACommented:
It's straight forward. If the last time the file was written to is older than 14 months ago, it gets deleted.
This will show the last write time of the files in question:
$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
	}

Open in new window

Or shorter (do not use this in a script; aliases should only be used directly in the console, never, ever, in scripts):
$limit = (Get-Date).AddDays(-426)
$path = "D:\Logs"
gci -Path $path -File -Recurse -Force | ? {$_.LastWriteTime -lt $limit} | % {"[$($_.LastWriteTime)] $($_.FullName)"; <# del -Path $_.FullName -Force -WhatIf #>}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

namergSystems AdministratorAuthor Commented:
So, the script will be

$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
	}

Open in new window


Right ?
0
oBdACommented:
Yes. It's still completely harmless, because -WhatIf is still passed to Remove-Item.
0
namergSystems AdministratorAuthor Commented:
Thank you sir
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.