PS script to remove files older than 2 months?

Hello,

I have a PS script that runs at 12am to clear down user profiles for our Citrix XenApp environment and looks like this:

Set-ExecutionPolicy Unrestricted -Force

 

remove-item "c:\UPM\andyw\UPM_Profile\AppData\Local\Microsoft\Windows\WER\ReportQueue\*" -recurse

remove-item "c:\UPM\andyw\UPM_Profile\AppData\Roaming\google\cache\Default\Application Cache\Cache\*" -recurse

remove-item "c:\UPM\andyw\UPM_Profile\AppData\Roaming\google\cache\Default\Pepper Data\*" -recurse

remove-item "c:\UPM\andyw\UPM_Profile\AppData\Roaming\google\cache\PepperFlash\*" -recurse

However there is another folder where I would like to keep the last 2 months of files and get rid of the rest how can I do this:

This is the folder:

C:\UPM\michaelc\UPM_Profile\AppData\Roaming\google\cache\Default\Local Storage

July 29th, 2015 9:23am

Get-ChildItem Path  C:\UPM\michaelc\UPM_Profile\AppData\Roaming\google\cache\Default\Local Storage Recurse | Where-Object CreationTime lt (Get-Date).AddDays(-60) | Remove-Item

you can use the -whatif to test it out

Free Windows Admin Tool Kit Click here and download it now
July 29th, 2015 9:40am

TB303, do you require an exact calendar definition of "2 months" ?

Or is 60 days good enough to approximate a span of 2 months?


July 29th, 2015 12:22pm

FD, two comments:
1) since there is a space in the value of -Path, you need to wrap the value in quotes
2) your code will attempt to remove folders as well as files

Free Windows Admin Tool Kit Click here and download it now
July 29th, 2015 12:26pm

Get-ChildItem Path  C:\UPM\michaelc\UPM_Profile\AppData\Roaming\google\cache\Default\Local Storage Recurse | Where-Object CreationTime lt (Get-Date).AddDays(-60) | Remove-Item

you can use the -whatif to test it out

July 29th, 2015 1:34pm

TB303, do you require an exact calendar definition of "2 months" ?

Or is 60 days good enough to approximate a span of 2 months?


  • Edited by LarryWeiss Wednesday, July 29, 2015 4:21 PM
Free Windows Admin Tool Kit Click here and download it now
July 29th, 2015 4:17pm

Hi Larry,

The last 60 days would be good

July 30th, 2015 3:12am

Interesting discussion of this topic is found at 

http://blog.danskingdom.com/powershell-functions-to-delete-old-files-and-empty-directories/
and
http://stackoverflow.com/questions/17829785/delete-files-older-than-15-days-using-powershell

There are a lot of things to consider:
1) Restricting it to deleting files but retaining folders
2) Whether you want it to search subdirectories and delete files found there
3) Whether you want it to delete hidden files
4) Do you need it to be compatible with PowerShell 2.0
5) Do you need it to continue processing other files if it encounters an error deleting a file
6) Do you want it to create a log of which files it finds are over 60 days old and the success with deleting them

Be sure to use the -Whatif switch when testing to minimize the unintentional damage such a script can do.

Free Windows Admin Tool Kit Click here and download it now
July 30th, 2015 9:16am

Interesting discussion of this topic is found at 

http://blog.danskingdom.com/powershell-functions-to-delete-old-files-and-empty-directories/
and
http://stackoverflow.com/questions/17829785/delete-files-older-than-15-days-using-powershell

There are a lot of things to consider:
1) Restricting it to deleting files but retaining folders
2) Whether you want it to search subdirectories and delete files found there
3) Whether you want it to delete hidden files
4) Do you need it to be compatible with PowerShell 2.0
5) Do you need it to continue processing other files if it encounters an error deleting a file
6) Do you want it to create a log of which files it finds are over 60 days old and the success with deleting them

Be sure to use the -Whatif switch when testing to minimize the unintentional damage such a script can do.

  • Edited by LarryWeiss Thursday, July 30, 2015 2:12 PM
July 30th, 2015 1:14pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics