Share via


Windows operations: How to extract folder statistics using PowerShell

It is an old saying, the necessity is the mother of invention, not that I have invented something, but for a rookie like me, it was exciting to try a programmatic approach to problems. I was having a lot of trouble with C: drive in windows. The size kept increasing and I never added any new software, made sure all data stays on my data drive.

It was actually the Windows.edb , search indexer which was bloating, consuming Gbs of space in G drive. It took me some time to find out this solution and hence I came up with the folder size extraction script. it pointed me to the C:\ProgramData and Windows.edb file.

The script which helped me extract the size and names of all the files is below.

For the solution to the search index issue

https://support.microsoft.com/en-in/help/2838018/the-windows.edb-file-grows-very-large-in-windows-8-or-windows-server-2012


[Script]



      Write-Host "`n Type the full location and folder name to extract details" -BackgroundColor Yellow -ForegroundColor Red  
      $Foldername = Read-Host  
      $Container = (Get-ChildItem -Recurse -force $Foldername -Exclude *.tmp)  
      $NumberItems = $container.length  
      $output= @()  
      $FolderCount=0  
      Write-Host "`n The number of files and directories in the given folder is $NumberItems" -ForegroundColor Green  
      $totalsize =[INT] 0  
      Foreach($Item in $Container)  
      {  
      $isfolder = "No"  
      Write-Host "`n Analyzing $item" -ForegroundColor Yellow  
      if($Item.mode -eq "d-----")  
      {  
      $Foldercount+=1  
      Write-Host "`n We found $FolderCount subfolder ($item)"  
      $isfolder = "Yes"  
      }  
      $option = [System.StringSplitOptions]::RemoveEmptyEntries  
      $fullpapth = (($Item).PSpath).split(":",2,$option)[1]  
      $ItemName = $Item.Name  
      $ItemSize ="{0:N2}" -f (($Item.Length)/(1024*1024))  
      $obj=New-Object PSobject  
      $obj | Add-Member Name $ItemName  
      $obj | Add-Member Folder $isfolder  
      $obj | Add-Member Size $ItemSize  
      $obj | Add-Member Location $fullpapth  
      $totalsize+=($ItemSize)  
      $output+=$obj  
      Start-Sleep -Seconds 1  
      }  
      $output | Format-Table  
      $output | Select-Object Name,Folder,Size,Location | Export-Csv Stats.csv -NoTypeInformation -Noclobber  
      Write-Host "Total Size of the directory $totalsize MB"