Managing Old Files with PowerShell

Managing disk space is a common task for IT professionals, especially when dealing with folders that accumulate temporary or outdated files. Using simple PowerShell Script, we can quickly check and delete files older than a given date.

In this post, I’ll cover:
Reviewing Files for Cleanup: Generate a count of files older than 30 days, grouped by folder.
Deleting Old Files: Automatically remove files older than a specified number of days, with an output showing the count of deleted files per folder.

1. Reviewing Old Files for Cleanup

This script will help you identify and review old files by counting how many files are older than 30 days in each folder within a specified directory.

PowerShell Script: Count Old Files by Folder

# Script: Count Old Files by Folder
# Set path to the root folder, and max age params
$rootFolder = "C:\myfolder"
$maxAge = 30

# get old file info
$cutoffDate = (Get-Date).AddDays(-$maxAge)
$report = Get-ChildItem -Path $rootFolder -Directory | ForEach-Object {
    $folder = $_.FullName
    $oldFiles = Get-ChildItem -Path $folder -File | Where-Object { $_.LastWriteTime -lt $cutoffDate }
    [PSCustomObject]@{
        FolderPath = $folder
        OldFileCount = $oldFiles.Count
    }
}

# Output the report
$report | Format-Table -AutoSize

Output Example

FolderPathOldFileCount
C:\myfolder\logs25
C:\myfolder\temp10

We can use this information to then verify what’s old within these directories.

2. Deleting Old Files with a PowerShell Script

Once you’ve reviewed the old files, this script can delete files older than a specified number of days. This script also outputs a count of deleted files per folder, helping you track cleanup progress.

PowerShell Script: Delete Old Files with Count

# Script: Delete Old Files with Count

# Set the path to the root folder you want to clean up
$rootFolder = "C:\myfolder"

# Set the maximum age of the files in days
$maxAge = 30
# Calculate the cutoff date
$cutoffDate = (Get-Date).AddDays(-$maxAge)

# Loop through each subfolder and delete old files
Get-ChildItem -Path $rootFolder -Directory | ForEach-Object {
    $folder = $_.FullName
    $oldFiles = Get-ChildItem -Path $folder -File | Where-Object { $_.LastWriteTime -lt $cutoffDate }
    $fileCount = $oldFiles.Count

    # Delete the files
    $oldFiles | Remove-Item -Force

    # Output the folder path and count of deleted files
    [PSCustomObject]@{
        FolderPath = $folder
        DeletedFileCount = $fileCount
    }
} | Format-Table -AutoSize

Notes:

Safety First:
Test both scripts in a non-critical environment before running them on production data.

Recycle Bin:
Files deleted with Remove-Item are permanently removed, not sent to the recycle bin.

Customizable Parameters:
You can adjust $maxAge and $rootFolder to fit your requirements.

Feel free to check out my other post, Advanced File and Folder Creation with PowerShell which includes more info on using PowerShell cmdlets to manage files!


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *