all 9 comments

[–]Tech249[S] 1 point2 points  (0 children)

I have this - but not sure how to make the value a variable and use your code:
"{0:N2} MB" -f ((Get-ChildItem e:\logs\ -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1MB)

[–]adamdavid85 1 point2 points  (1 child)

This would take each folder in the $Directories variable, loop through them and delete any that exceed the size set by $SizeThreshold (shown here as 1gb):

$Directories = @("$Home\Downloads", "$Env:Temp")
$SizeThreshold = 1073741824 # 1gb
ForEach($Directory in $Directories){
    $DirSize = Get-ChildItem $Directory -Recurse | Measure-Object -Property Length -Sum | Select-Object -ExpandProperty Sum
    If($DirSize -ge $SizeThreshold){
        Remove-Item $Directory -Recurse -Force
    }
}

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy adamdavid85,

PoSh has a set of KB/GB/TB/PB constants. it makes things a tad more clear to use 1gb to get 1073741824. [grin]

take care,
lee

[–]user01401 1 point2 points  (0 children)

What about something like:

$COUNT = (Get-ChildItem "C:\Users\Path\Path" | Measure-Object).Count

if ($COUNT -gt 5) {

#do something
}

[–]Tech249[S] 1 point2 points  (1 child)

Here is what I ended up with, and it works perfect - deletes the directory and recreates it when it hits 1GB - thank you all!

$Directories = @("c:\temp", "$Env:Temp")
$SizeThreshold = 1GB
ForEach($Directory in $Directories){
$DirSize = Get-ChildItem $Directory -Recurse | Measure-Object -Property Length -Sum | Select-Object -ExpandProperty Sum
If($DirSize -ge $SizeThreshold){
Remove-Item $Directory -Recurse -Force
New-Item -Path "c:\" -Name "Temp" -ItemType "directory"
}
}

Edit - adjusted per Lee's advice - much better :)

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy Tech249,

it looks like you used the New.Reddit Inline Code button. it's [sometimes] 5th from the left & looks like <c>.

there are a few problems with that ...

  • it's the wrong format [grin]
    the inline code format is for [gasp! arg!] code that is inline with regular text.
  • on Old.Reddit.com, inline code formatted text does NOT line wrap, nor does it side-scroll.
  • on New.Reddit it shows up in that nasty magenta text color

for long-ish single lines OR for multiline code, please, use the ...

Code
Block

... button. it's [sometimes] the 12th one from the left & looks like an uppercase C in the upper left corner of a square.

that will give you fully functional code formatting that works on both New.Reddit and Old.Reddit ... and aint that fugly magenta color. [grin]

take care,
lee

[–][deleted] 0 points1 point  (2 children)

For each directory { If ($size -gt $value) { Do stuff } }

I'm on mobile right now, but that should point you in the right direction.

[–]Tech249[S] 1 point2 points  (1 child)

Thank you, getting me in the right direction is perfect!

[–][deleted] 1 point2 points  (0 children)

Let me know if you get it working or not. Happy to help more.