# encoding: ascii
# api: powershell
# title: get-outlookFolderSize
# description: Enumerate through the default folder within Outlook and calculate size of each sub-folder and produce a final total size.
# version: 0.1
# type: function
# author: marcadamcarter
# license: CC0
# function: get-folderItems
# x-poshcode-id: 2641
# x-archived: 2011-05-03T14:16:40
# x-published: 2011-04-30T13:24:00
#
# Appreciate any feedback on optimizing this (e.g instead of summing up size of each message).
#

function get-folderItems
{
    Param($parent)
    #Iterate each Message, add up Size of all messages
    $parent.Items | % { $script:intSize += $_.Size }
    $currentSize = "{0:N2}" -f (($script:intSize - $script:prevSize)/1MB)
    $obj = New-Object PsObject
    $obj | Add-Member -memberType NoteProperty "Folder" -Value $parent.FolderPath
    $obj | Add-Member -memberType NoteProperty "Weight(MB)" -Value $currentSize
    $obj 
    $script:array += $obj
    $script:prevSize = $script:intSize
    
    #Enumerate Subfolders
    foreach($folder in $parent.Folders)
    {
        get-folderItems $folder
    }
}

$array = @()
$intSize = 0
$prevSize = 0
$o = new-object -comobject outlook.application 
$n = $o.GetNamespace("MAPI")
$objFolder = $n.GetDefaultFolder("olFolderInbox")
get-folderItems $objFolder

#Add Total to object before displaying final result
$obj = New-Object PsObject
$obj | Add-Member -memberType NoteProperty "Folder" -Value "(Total)"
$obj | Add-Member -memberType NoteProperty "Weight" -Value $("{0:N2}" -f ($intSize/1MB))
$script:array += $obj
$array | ft -auto
