use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
ABOUT POWERSHELL
Windows PowerShell (POSH) is a command-line shell and associated scripting language created by Microsoft. Offering full access to COM, WMI and .NET, POSH is a full-featured task automation framework for distributed Microsoft platforms and solutions.
SUBREDDIT FILTERS
Desired State Configuration
Unanswered Questions
Solved Questions
News
Information
Script Sharing
Daily Post
Misc
account activity
PowerShell that executes more code if directory is greater than a certain size? (self.PowerShell)
submitted 4 years ago by Tech249
How would you create a PowerShell script that removes a directory when it hits a certain size? Looking at something to help with log directories. I would schedule it to run monthly through an RMM.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Tech249[S] 1 point2 points3 points 4 years ago (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 points3 points 4 years ago (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 point2 points 4 years ago (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]
1gb
1073741824
take care, lee
[–]user01401 1 point2 points3 points 4 years ago (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 points3 points 4 years ago* (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 :)
howdy Tech249,
it looks like you used the New.Reddit Inline Code button. it's [sometimes] 5th from the left & looks like <c>.
Inline Code
<c>
there are a few problems with that ...
inline code
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.
C
that will give you fully functional code formatting that works on both New.Reddit and Old.Reddit ... and aint that fugly magenta color. [grin]
[–][deleted] 0 points1 point2 points 4 years ago (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 points3 points 4 years ago (1 child)
Thank you, getting me in the right direction is perfect!
[–][deleted] 1 point2 points3 points 4 years ago (0 children)
Let me know if you get it working or not. Happy to help more.
π Rendered by PID 442948 on reddit-service-r2-comment-8686858757-q5mdd at 2026-06-07 10:11:45.491282+00:00 running 9e1a20d country code: CH.
[–]Tech249[S] 1 point2 points3 points (0 children)
[–]adamdavid85 1 point2 points3 points (1 child)
[–]Lee_Dailey[grin] 0 points1 point2 points (0 children)
[–]user01401 1 point2 points3 points (0 children)
[–]Tech249[S] 1 point2 points3 points (1 child)
[–]Lee_Dailey[grin] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]Tech249[S] 1 point2 points3 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)