Skip to content

Instantly share code, notes, and snippets.

@withinboredom
Created June 9, 2014 03:14
Show Gist options
  • Save withinboredom/0806d77dd317d657959d to your computer and use it in GitHub Desktop.
Save withinboredom/0806d77dd317d657959d to your computer and use it in GitHub Desktop.
Deletes a file if over a certain size
#pick your file
$Dir = "F:\MyDir\stuff.cache"
#Max size in megs (gig = 1024 megs
$SizeMax = 1024
#get's the size in mb of the file(s)
$Size = (Get-ChildItem $Dir| Measure-Object -property length -sum)
# convert it to megs
$SizeMb="{0:N2}" -f ($size.sum / 1MB) + "MB"
# delete if calulated size is greater than set size above
if ($sizeMb -ge $sizeMax) {
# and delete them
Get-ChildItem $dir -Recurse | Remove-Item –Force
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment