PowerShell script to keep latest 10 images on self-hosted agent and remaining need to delete

Viewed 32

I need a PowerShell script, always keep latest 10 images and other images need to delete automatically in linux based self-hosted agent VM. This script I need to pass via azure pipeline task like AZ CLI inline script. Can any one help me out this. enter image description here I need to keep latest 10 docker images

Thanks in advance...

1 Answers

The following should be a base for a script. I assume that you can look up elsewhere how to turn it into a proper cmdlet if you need that:

$path = "..."
$fileTypes = @('bmp', 'png', 'jpg') # * for all file
$filesToKeep = 10


Get-ChildItem ($fileTypes |% { join-path $path ('*.' + $_) }) | sort -Property LastWriteTime -Descending  | select -skip $filesToKeep | Remove-Item -Force

Use CreationTime over LastWriteTime if you prefer to go by date created, versus last modified.

Related