This script was created to keep a folder or drive below a configured threshold.

This script deleted the oldest files from the folder or drive you specify. 

In its current unmodified configuration, it is set to keep the security_Camera folder on my K:\ drive above 10% free space. This is accomplished by scanning the folding or drive to figure out the used space percentage, if the used space percentage is less than the configured $FreeSpaceThreshold then it will remove the oldest file, rescan, if needed and remove the oldest file again. This will continue to happen until the $PercentageFree is over the threshold.

 

 

PowerShell
Edit|Remove
    [Cmdletbinding(SupportsShouldProcess)] 
 
        Param( 
                [Parameter(Mandatory = $false, 
                           ValueFromPipelineByPropertyName = $true, 
                           Position = 0, 
                           HelpMessage = "Please enter the full path of the folder or drive you would like to monitor and clean")] 
                           [string[]]$Path = "K:\Security_Camera", 
                [Parameter(Mandatory = $false, 
                           ValueFromPipelineByPropertyName = $true, 
                           Position = 1, 
                           HelpMessage = "What free space percentage do you want to set the threshold for?")] 
                           [int]$FreeSpaceThreshold = "10", # What percentage do you want the drive to free up until? 
                [Parameter(Mandatory = $false, 
                           ValueFromPipelineByPropertyName = $true, 
                           Position = 2, 
                           HelpMessage = "Please enter the full path to where you would like a transaction log stored.")] 
                           $TranscriptLog = "$env:TEMP\Automated-FolderCleanup.log" 
        ) 
        Begin{ 
                Start-Transcript -Path $TranscriptLog 
                [string]$Drive = Split-Path -Qualifier $Path 
                [WMI]$Disk = "Win32_LogicalDisk.DeviceID='$Drive'" 
                [int]$PercentFree = $Disk.FreeSpace / $Disk.Size * 100 
        } 
 Please download the full script!