Difference between revisions of "Windows Troubleshooting"
Jump to navigation
Jump to search
(Created page with "- https://www.reddit.com/r/PowerShell/comments/66nsr9/treesize_script/") |
|||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | ``` | ||
+ | rm -r -fo C:\Users\my user | ||
+ | Remove-Item -Recurse -Force some_dir | ||
+ | ``` | ||
+ | |||
- https://www.reddit.com/r/PowerShell/comments/66nsr9/treesize_script/ | - https://www.reddit.com/r/PowerShell/comments/66nsr9/treesize_script/ | ||
+ | |||
+ | ``` | ||
+ | function getSubFolderSizes | ||
+ | { | ||
+ | param( | ||
+ | [string]$targetFolder, | ||
+ | [Int64]$minSize = 1GB, | ||
+ | [Int64]$parentSize, | ||
+ | [String]$indent = " ", | ||
+ | [Int]$depth = 1 | ||
+ | ) | ||
+ | |||
+ | if ($depth -le 3){ #depths greater than this threshold cause the function to return $null, signaling that the higher level folder should be used | ||
+ | |||
+ | # Get a list of Sub-directories in the current targetFolder | ||
+ | $colItems = Get-ChildItem $targetFolder -Directory -ErrorAction SilentlyContinue | Where-Object{$_.FullName.Substring(0,1) -eq $targetFolder.Substring(0,1)} | ||
+ | |||
+ | # Check each sub-folder | ||
+ | foreach ($i in $colItems){ | ||
+ | |||
+ | # Measure the size of all Files in this subfolder | ||
+ | Get-ChildItem -Depth 1000 -LiteralPath $i.FullName -File -Force -Recurse -ErrorAction SilentlyContinue | Measure-Object -property length -sum -ErrorAction SilentlyContinue | | ||
+ | Where-Object {$_.Sum -ge $minSize} | | ||
+ | ForEach-Object { | ||
+ | # This subfolder's size is above threshold | ||
+ | |||
+ | # Add it to our output | ||
+ | ("{0,15}{1,7}{2,-1}" -f ("{0:N2}" -f ($_.sum / 1GB) + " GB"), ("{0:P0}" -f ($_.sum / ($lowestVolume.Size - $lowestVolume.SizeRemaining)) + " "), ($indent + $i.FullName)) | ||
+ | |||
+ | #And dig deeper | ||
+ | getSubFolderSizes $i.FullName $minSize $_.sum (" " + $indent) ($depth+1) | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | $minFolderSize = 1GB | ||
+ | $lowestVolume = Get-Volume C | ||
+ | getSubFolderSizes ($lowestVolume.DriveLetter+":\") $minFolderSize ($lowestVolume.Size - $lowestVolume.SizeRemaining) | ||
+ | ``` | ||
+ | |||
+ | and performant | ||
+ | |||
+ | ``` | ||
+ | param( | ||
+ | [String]$Path, | ||
+ | [Int]$Count = 10, | ||
+ | [Int64]$MinSize = 1GB | ||
+ | ) | ||
+ | |||
+ | $tempOutput = New-Object PSObject -Property @{Length=0; Path=$null} | ||
+ | |||
+ | Get-ChildItem -Depth 1000 -LiteralPath $path -File -Force -Recurse -ErrorAction SilentlyContinue | Select-Object Directory, Length | | ||
+ | ForEach-Object{ | ||
+ | if ($_.Directory.FullName -eq $tempOutput.Path){ | ||
+ | $tempOutput.Length += $_.Length | ||
+ | } else { | ||
+ | if ($tempOutput.Length -ge $minSize){ | ||
+ | $tempOutput | select Path, @{Name="Size (GB)"; Expression={"{0:N2}" -f ($_.Length / 1GB)}} | ||
+ | } | ||
+ | |||
+ | $tempOutput.Length = 0 | ||
+ | $tempOutput.Path = $_.Directory.FullName | ||
+ | } | ||
+ | } | | ||
+ | Sort-Object "Size (GB)" -Descending | Select-Object -First $Count | ||
+ | ``` |
Latest revision as of 22:03, 16 March 2020
rm -r -fo C:\Users\my user Remove-Item -Recurse -Force some_dir
function getSubFolderSizes { param( [string]$targetFolder, [Int64]$minSize = 1GB, [Int64]$parentSize, [String]$indent = " ", [Int]$depth = 1 ) if ($depth -le 3){ #depths greater than this threshold cause the function to return $null, signaling that the higher level folder should be used # Get a list of Sub-directories in the current targetFolder $colItems = Get-ChildItem $targetFolder -Directory -ErrorAction SilentlyContinue | Where-Object{$_.FullName.Substring(0,1) -eq $targetFolder.Substring(0,1)} # Check each sub-folder foreach ($i in $colItems){ # Measure the size of all Files in this subfolder Get-ChildItem -Depth 1000 -LiteralPath $i.FullName -File -Force -Recurse -ErrorAction SilentlyContinue | Measure-Object -property length -sum -ErrorAction SilentlyContinue | Where-Object {$_.Sum -ge $minSize} | ForEach-Object { # This subfolder's size is above threshold # Add it to our output ("{0,15}{1,7}{2,-1}" -f ("{0:N2}" -f ($_.sum / 1GB) + " GB"), ("{0:P0}" -f ($_.sum / ($lowestVolume.Size - $lowestVolume.SizeRemaining)) + " "), ($indent + $i.FullName)) #And dig deeper getSubFolderSizes $i.FullName $minSize $_.sum (" " + $indent) ($depth+1) } } } } $minFolderSize = 1GB $lowestVolume = Get-Volume C getSubFolderSizes ($lowestVolume.DriveLetter+":\") $minFolderSize ($lowestVolume.Size - $lowestVolume.SizeRemaining)
and performant
param( [String]$Path, [Int]$Count = 10, [Int64]$MinSize = 1GB ) $tempOutput = New-Object PSObject -Property @{Length=0; Path=$null} Get-ChildItem -Depth 1000 -LiteralPath $path -File -Force -Recurse -ErrorAction SilentlyContinue | Select-Object Directory, Length | ForEach-Object{ if ($_.Directory.FullName -eq $tempOutput.Path){ $tempOutput.Length += $_.Length } else { if ($tempOutput.Length -ge $minSize){ $tempOutput | select Path, @{Name="Size (GB)"; Expression={"{0:N2}" -f ($_.Length / 1GB)}} } $tempOutput.Length = 0 $tempOutput.Path = $_.Directory.FullName } } | Sort-Object "Size (GB)" -Descending | Select-Object -First $Count