Monday, December 31, 2018

PowerShell Folder Size Checker

PowerShell folder size checker. 

I use this on Windows 2012R2. A small script to analyze the size of each sub-directory. Output is in a text file, recommend using Excel to analyze the output.

#######################################
#
# Get tree like drive size output
#
# Modify the last line to reflect the 
#  folder starting point and the depth
#  of sub-folders to output
#
# Your mileage will vary depends on 
#  the permissions you have to 
#  sub-directories.
#  But you can do the math afterward.
########################################
# Create a new blank output file
$log = "e:\temp\foldersize.txt"
out-file -FilePath $log -InputObject ""

# This is the main function and within it a recursive calls
function get-folderSize(){
    param(
        $path = "e:\temp",
        $depth = 3
    )
    $log = "e:\temp\foldersize.txt"
    $output = ""
    $output = Get-ChildItem -path $path -ErrorAction SilentlyContinue
    $totalSize = 0
    #look through every item individually
    foreach($item in $output){
        #If it is a folder, then call a nested function of itself
        if($item.psIsContainer -eq $true){
            $totalSize = $totalSize + (Get-folderSize -Path $item.fullname -depth ($depth-1))
        }else{
            $totalSize = $totalSize + $item.length
        }
    }
    #As long as depth is greater than 0 then present the files in that depth in the output
    # Use the delimiter of double colon when parsing output
    if($depth -ge 0){
        $inputObject = $path + "::" + [math]::round($totalSize/1MB,2) + "::" + $depth
        out-file -FilePath $log -InputObject $inputObject -Append
    }
    return $totalSize
}
get-folderSize -path C:\Windows -depth 1


Sample output:

... 
C:\Windows\System::0.03::0
C:\Windows\System32::2885.63::0
C:\Windows\SystemResources::3.23::0
C:\Windows\SysWOW64::992.38::0
C:\Windows\TAPI::0::0
C:\Windows\Tasks::0::0
C:\Windows\Temp::1525.79::0
C:\Windows\ToastData::0.01::0
C:\Windows\tracing::0::0
C:\Windows\Vss::0.01::0
C:\Windows\Web::2.68::0
C:\Windows\WinSxS::7392.18::0
C:\Windows::15796.69::1

No comments:

Post a Comment

AWS WAF log4j query

How to query AWS WAF log for log4j attacks 1. Setup your Athena table using this instruction https://docs.aws.amazon.com/athena/latest/ug/wa...