Delete all files with *.zip except latest 5(recent) files under windows server

I have filenames called

abc_1.zip
abc_2.zip
abc_3.zip
abc_4.zip
abc_5.zip(below_latest)
abc_6.zip(latest)

under windows server 2012 r2. Now I need a single command to delete all files except the latest and below_latest one.

Note: The above names are just for example. So I can say I want to keep the only recent file(latest and below_latest) and remove all.

Also, I need a single line command, not a PowerShell script.

Thanks in advance.


Solution 1:

Single Line PowerShell:

Get-ChildItem -file | Sort-Object LastWriteTime -Descending | Select-Object -Skip 2 | Remove-Item

or using some aliases:

ls -file | sort LastWriteTime -d | Select -Skip 2 | rm

if you are not in the directory with the files to delete use:

Get-ChildItem -Path C:\test ...