If you need to automatically delete files and folders when disk space on your drive falls below a certain threshold, you can use a PowerShell script to accomplish this task. Here’s how you can set it up.
PowerShell Script
This script will delete files and directories under a specified path when the available disk space is less than 5 GB.
# Define the path to the directory
$directoryPath = "C:\DeleteFilesFolder\
# Set the threshold for disk space (in bytes)
$threshold = 5GB
# Get the drive information
$drive = Get-PSDrive -PSProvider FileSystem | Where-Object { $_.Root -eq "C:\" }
# Check the available free space on the drive
if ($drive.Free -lt $threshold) {
# Get all files in the specified directory and subdirectories
$files = Get-ChildItem -Path $directoryPath -Recurse -File
# Loop through and delete each file
foreach ($file in $files) {
Remove-Item -Path $file.FullName -Force
Write-Output "Deleted file: $($file.FullName)"
}
# Get all directories in the specified directory and subdirectories
$directories = Get-ChildItem -Path $directoryPath -Recurse -Directory | Sort-Object -Property FullName -Descending
# Loop through and delete each directory if it is empty
foreach ($directory in $directories) {
Remove-Item -Path $directory.FullName -Recurse -Force
Write-Output "Deleted directory: $($directory.FullName)"
}
Write-Output "All files and directories in $directoryPath and its subdirectories have been deleted due to low disk space."
} else {
Write-Output "Sufficient disk space available: $($drive.Free / 1GB) GB free."
}
Explanation of the Script
- $directoryPath: Specifies the directory path where files and folders will be deleted.
- $threshold: Sets the disk space threshold to 5 GB.
- $drive: Checks the free space available on the C: drive.
- if ($drive.Free -lt $threshold): Checks if the available free space is less than 5 GB.
- Get-ChildItem -Path $directoryPath -Recurse -File: Gets all files in the specified directory and subdirectories.
- Remove-Item -Path $file.FullName -Force: Deletes each file without user interaction.
- Get-ChildItem -Path $directoryPath -Recurse -Directory | Sort-Object -Property FullName -Descending: Gets all directories in descending order to ensure deletion from the deepest level first.
- foreach ($directory in $directories): Deletes each directory and its contents without user interaction if it’s empty.
Running the Script Manually
- Open PowerShell as Administrator.
- Run the Script:
C:\Scripts\ClearIncomingQueue.ps1
Automating the Script with Task Scheduler
You can use Task Scheduler to automate the script.
- Open Task Scheduler: Open it by typing
taskschd.msc
in the Run dialog. - Create a New Task:
- Right-click and select “Create Task”.
- General tab: Name your task (e.g., “Clear Files and Folders”).
- Triggers tab: Click “New” and choose the schedule (e.g., daily).
- Actions tab: Click “New” and select “Start a program”. Enter
powershell.exe
as the program/script. - In the “Add arguments (optional)” field, enter:
-File "C:\Scripts\ClearIncomingQueue.ps1"
- Conditions tab: Check “Start the task only if the computer is on AC power”.
- Settings tab: Check “If the task fails, restart every” and set an interval.