Skip to content

Instantly share code, notes, and snippets.

@peteraritchie
Created August 26, 2026 18:22
Show Gist options
  • Select an option

  • Save peteraritchie/2b0bafabb8483ebb22cb9f94b018de6d to your computer and use it in GitHub Desktop.

Select an option

Save peteraritchie/2b0bafabb8483ebb22cb9f94b018de6d to your computer and use it in GitHub Desktop.
Powershell script to remove all '.trx' and '.coverage' files from 'TestResults' directories and delete the directories if they are empty
<#PSScriptInfo
.VERSION 1.0.0
.AUTHOR Peter Ritchie
.COMPANYNAME
.COPYRIGHT
.TAGS
.LICENSEURI
.PROJECTURI
.ICONURI
.EXTERNALMODULEDEPENDENCIES
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES
.PRIVATEDATA
#>
<#
.SYNOPSIS
Removes all '.trx' and '.coverage' files from 'TestResults' directories and deletes the directories if they are empty.
.PARAMETER Path
The root path to start searching for 'TestResults' directories.
.EXAMPLE
.\Remove-TestResults.ps1 -Path "$env:USERPROFILE\source\repos\MyProject"
#>
[CmdletBinding(SupportsShouldProcess)]
param(
[Parameter(Mandatory=$true)]
[string]$Path
)
Get-ChildItem -Path $Path -Recurse -Directory -Filter 'TestResults' | ForEach-Object {
$testResultsDir = $_.FullName
Write-Verbose "Checking $($testResultsDir)...";
#
foreach($f in Get-ChildItem -Path $testResultsDir -Recurse -Include '*.trx', '*.coverage')
{
if($PSCmdlet.ShouldProcess($Path, "Delete trx/coverage file $($f.FullName)"))
{
Write-Verbose "Deleting trx/coverage file $($f.FullName)";
Remove-Item -Path $f.FullName -Force
}
}
if (-Not (Get-ChildItem -Path $testResultsDir -Recurse -File)) {
if($PSCmdlet.ShouldProcess($Path, "Remove empty folder $testResultsDir"))
{
Write-Verbose "Removing empty TestResults folder $testResultsDir";
Remove-Item -Path $testResultsDir -Recurse -Force
}
} else {
if ($PSBoundParameters['Verbose'] -or $VerbosePreference -eq 'Continue') {
Write-Host "$testResultsDir is not empty" -ForegroundColor Red
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment