Windows – Powershell: Single Script to “clean” multiple folders of files older than

filesystemspowershellpowershell-v3.0scriptingwindows

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.

Best Answer

Edited to include your requested text file of

c:\logs\iis\siteA\ -30
c:\logs\job1\ -60
e:\archive\clientA\ -90

I tested the new bits of code separately from what I had lying around, but it should still do what you want, I think.

$controlfile = "c:\path\to\yourfile.txt"    
$curr_date = Get-Date

New-Item -ItemType directory -Path f:\delete

foreach ($line in get-content $controlfile)
{
    $split = $line.split(" ")
    $file_path = $split[0]
    $max_days = $split[1]

    $del_date = $curr_date.AddDays($max_days)

    # move the files to the kill directory
    Get-ChildItem $file_path | Where-Object { $_.LastWriteTime -lt $del_date } |  Move-Item -destination "f:\delete"
}

The reason it moves it to a different directory is because there's apparently a bug in recursive deletes, and my data is many, many subdirectories deep, so once I've got everything moved to the kill directory, I run this:

del /f/s/q f:\delete > nul
rmdir /s/q f:\delete

If you don't have that problem, you could instead add this to the end of the above bit of PowerShell:

$del_dir = "f:\delete"
$fso = New-Object -ComObject scripting.filesystemobject
$fso.DeleteFolder($del_dir)