Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save blakedrumm/62a572ddc786963136641b2c52894211 to your computer and use it in GitHub Desktop.
Save blakedrumm/62a572ddc786963136641b2c52894211 to your computer and use it in GitHub Desktop.
PowerShell script to remove WindowsPatchExtension status files with indexes higher than the latest settings file, for Update Manager cleanup.
<#
.SYNOPSIS
Removes status files with indexes greater than the highest index found in the settings directory.
.DESCRIPTION
This script automatically finds the latest versioned extension directory for the WindowsPatchExtension,
identifies the highest-numbered `.settings` file under the `RuntimeSettings` directory, and deletes all
`.status` files under the corresponding `status` directory that have a higher numeric index.
.PARAMETER None
No parameters required. Paths are dynamically resolved from the base extension root.
.NOTES
Author: Blake Drumm ([email protected])
Created: June 13th, 2025
License: MIT License - Microsoft
MIT License
Copyright (c) Microsoft Corporation
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
#>
# ============================
# PowerShell Script to Remove Status Files Above Max Settings Index
# ============================
# ----- Root Path and Wildcard Handling -----
$rootPath = "C:\Packages\Plugins\Microsoft.CPlat.Core.WindowsPatchExtension"
$settingsDir = Get-ChildItem -Path $rootPath -Directory | Where-Object {
Test-Path "$($_.FullName)\RuntimeSettings"
} | Sort-Object Name -Descending | Select-Object -First 1
$statusDir = Get-ChildItem -Path $rootPath -Directory | Where-Object {
Test-Path "$($_.FullName)\status"
} | Sort-Object Name -Descending | Select-Object -First 1
if (-not $settingsDir -or -not $statusDir) {
Write-Output "RuntimeSettings or Status directory not found under: $rootPath"
exit 1
}
$SettingsDirectory = Join-Path $settingsDir.FullName "RuntimeSettings"
$StatusDirectory = Join-Path $statusDir.FullName "status"
Write-Output "Using Settings Directory: $SettingsDirectory"
Write-Output "Using Status Directory: $StatusDirectory"
# ----- Get Highest Settings File Number -----
$settingsFiles = Get-ChildItem -Path $SettingsDirectory -File -Filter '*.settings'
$settingsNumbers = $settingsFiles.Name | ForEach-Object {
if ($_ -match '^(\d+)\.settings$') {
[int]$matches[1]
}
}
if (-not $settingsNumbers) {
Write-Output "No valid settings files found in: $SettingsDirectory"
exit 1
}
$maxSettingNumber = ($settingsNumbers | Measure-Object -Maximum).Maximum
Write-Output "Highest settings file index: $maxSettingNumber"
# ----- Remove Status Files With Numbers Greater Than Max -----
$statusFiles = Get-ChildItem -Path $StatusDirectory -File -Filter '*.status'
foreach ($file in $statusFiles) {
if ($file.Name -match '^(\d+)\.status$') {
$statusNum = [int]$matches[1]
if ($statusNum -gt $maxSettingNumber) {
try {
Remove-Item -Path $file.FullName -Force
Write-Output "Deleted: $($file.Name)"
} catch {
Write-Output "Failed to delete: $($file.Name). Error: $_"
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment