I would like to create a maintenance script that would run on each of our servers to clean out common drop/archive directories. Preferably the script would use a reference file for the folder path and desired aging limit. Then the script would clean that path of files older than the aging limit. The input reference file would look something like this:
c:\logs\iis\siteA\ 30
c:\logs\job1\ 60
e:\archive\clientA\ 90
The first component is the file path; and the second is the number of days files should be retained, separated by a space.
Now for the script I have the following; however, I am embarrassingly deficient in scripting experience. (i am more networking oriented)
#Attempted loop
Foreach ($ParentDir = Get-Content .\paths.txt $arg1, $arg2)
{$limit = (Get-Date).AddDays(-$arg2)
$path = $ParentDir
# Delete files older than the $limit.
Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force}
I feel like the logic is close but my syntax is off. Is my approach doable? is there a better way? Could you help with the syntax? Using poweshell v3.
Credit where credit is due lots of the logic i took from deadlydog from this post.