Behind the PowerShell Pipeline

Behind the PowerShell Pipeline

Archives
Log in
Subscribe
November 17, 2026

Trimming Files With PowerShell

In this issue:

  • Overview
  • Introduction
  • Generating Test Files
  • Selecting Files by Date
  • Selecting by Size
  • Using a Regex pattern
  • Remove-FileByPattern
  • Summary

Overview

I discuss how I manage to trim a set of like-named files using PowerShell. This is handy for trimming backups to set number of versions.

Introduction

I thought I'd share something I use to manage my backup files. My backup process generates archive files with a consistent naming pattern that includes a date reference. However, I only want to keep the four most recent backup logs which means I need to trim the file collection. This isn't especially difficult but there's always something to learn from examining how a problem is solved using PowerShell.

Generating Test Files

Naturally, I don't want to develop something using "production" files, so I'll start with some code to generate a set of test files.

$Path = "C:\temp"
#create 10 files
1..10 | Foreach {
    #define a file name
    $f = Join-Path -path $path -child "demo-data-$_.dat"
    #create a file with dummy content
    1..($_ * 10) | Set-Content $f
    #sleep a bit between files to widen the timestamps
    Start-Sleep -Seconds (Get-Random -min 1 -max 10)
}

After a few minutes I have these files to work with:

PS C:\> dir c:\temp\demo-*.dat

    Directory: C:\temp

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---           11/4/2026  7:31 AM             31 demo-data-1.dat
-a---           11/4/2026  7:32 AM            392 demo-data-10.dat
-a---           11/4/2026  7:31 AM             71 demo-data-2.dat
-a---           11/4/2026  7:31 AM            111 demo-data-3.dat
-a---           11/4/2026  7:31 AM            151 demo-data-4.dat
-a---           11/4/2026  7:31 AM            191 demo-data-5.dat
-a---           11/4/2026  7:31 AM            231 demo-data-6.dat
-a---           11/4/2026  7:32 AM            271 demo-data-7.dat
-a---           11/4/2026  7:32 AM            311 demo-data-8.dat
-a---           11/4/2026  7:32 AM            351 demo-data-9.dat

My production files are generated weekly so the time stamp is more dramatic, but this will suffice.

Selecting Files by Date

My normal process is to keep the 4 most recently created files. The critical element in this process is that the files need to follow a predictable naming pattern because there are other files in the same directory. You can see such pattern in the example above. I can use that to select and sort the files.

PS C:\> $Keep = 5
PS C:\> $pattern = "$Path\demo-data*.dat"
PS C:\> Get-ChildItem $pattern | Sort-Object LastWriteTime -Descending | Select-Object -skip $keep

    Directory: C:\temp

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---           11/4/2026  7:31 AM            191 demo-data-5.dat
-a---           11/4/2026  7:31 AM            151 demo-data-4.dat
-a---           11/4/2026  7:31 AM            111 demo-data-3.dat
-a---           11/4/2026  7:31 AM             71 demo-data-2.dat
-a---           11/4/2026  7:31 AM             31 demo-data-1.dat

I could have also used the CreationTime property. Regardless, these are the files I want to remove. I've sorted the files in descending order and skipping the X number I want to keep. Everything else is fair game to delete.

PS C:\> Get-ChildItem $pattern | Sort-Object LastWriteTime -Descending | Select-Object -skip $keep | Remove-Item -WhatIf

What if: Performing the operation "Remove File" on target "C:\temp\demo-data-5.dat".
What if: Performing the operation "Remove File" on target "C:\temp\demo-data-4.dat".
What if: Performing the operation "Remove File" on target "C:\temp\demo-data-3.dat".
What if: Performing the operation "Remove File" on target "C:\temp\demo-data-2.dat".
What if: Performing the operation "Remove File" on target "C:\temp\demo-data-1.dat".

I have never needed to save the X number of oldest files so I'll skip that variation.

Want to read the full issue?
Already a paid subscriber? Click here to log in.
GitHub
Bluesky
LinkedIn
Mastodon
jdhitsolutions.github.io
Powered by Buttondown, the easiest way to start and grow your newsletter.