I'm not sure if this would be considered a strange request, but I'm stuck on a script to search subfolders, and delete the smallest file from inside the folder.
I'm working with a folder structure along the lines of:
TopFolder
└─── folder1
│ │ file1.txt - 42kb
│ │ file2.txt - 84kb
| |
└─── folder2
│ file1.txt - 83kb
│ file2.txt - 34kb
...
I'm looking to find a way to recursively go through all the subfolders under the "TopFolder", find the smallest file in each of the folders, delete it, then continue to next folder.
I tried something along the lines of the following, but it's giving me "item doesn't exist" errors
Get-ChildItem -Recurse -Directory -Path 'Z:\TopFolder' |
ForEach-Object{
Get-ChildItem -File -Path $_ |
Where-Object { $_.Name -ne $(Split-Path -Path $PSCommandPath -Leaf) } |
Sort-Object -Property Length |
Select-Object -First 1 |
Remove-Item -WhatIf
}