I am trying to package only files that have been changed in the last 2 minutes using the built-in Team Foundation Server (TFS) build functionality from a git repository on the same TFS server.
I have written a PowerShell script to remove all old files that have a LastWriteTime older than 2 minutes:
# Get the current working directory
$currentDirectory = Get-Location
# Append the target directory to the current working directory
$targetDirectory = Join-Path $currentDirectory "CFM Reporting Project\SSRS\Reports"
# Get all files in the target directory and its subdirectories
Get-ChildItem -Recurse $targetDirectory |
Where-Object {$_.LastWriteTime -lt (Get-Date).AddMinutes(-2)} |
Remove-Item -Recurse -Force
# List all in target directory and its subdirectories
Get-ChildItem -Recurse $targetDirectory | Format-List -Property FullName,CreationTime,LastWriteTime,CheckinDate -Force
I have ensured the build definition sets the Clean option to false.
However, when building, no files are deleted because their LastWriteTimes are ALL the datetime at which the build started:
Here are further logs which show files since the above screenshot only shows folders:

I've used these sources for guidance and exploration of ideas:
- get-a-set-of-files-that-have-been-modified-after-a-certain-date
- tfs-2017-how-build-deliver-only-changed-files
- deploy-only-changed-files-in-tfs-deployment-2012
Extra information
- The deployment package consists of a repository of reports, not a Visual Studio solution file
- Microsoft Visual Studio Team Foundation Server Version 16.131.28601.4
- PowerShell Version 1.2.3
Can anyone give advice om how to package only the reports with changes in the last 2 minutes, especially since the LastWriteTime does is not working correctly.


Since you set the clean option to false, the pipeline will incrementally checkout changes of the repo.
From your screenshot about the date of the files, it shows change time at folder level not subfiles.
When sub-files in a folder change, the date of the folder will be updated together.
For example:
Subfiles:
Folder:
To package only the reports with changes in the last 2 minutes, you can check the date of the subfiles of the folder. Then you can package the related files.