Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Looping through Outlook folders and subfolders, returning number of e-mails

328 views
Skip to first unread message

Uwe Ziegenhagen

unread,
Sep 27, 2009, 7:48:01 AM9/27/09
to
Hi,

I would like to know the number of e-mails in each of my Outlook folders
but somehow I can't get the point: How can I go through the list of
folders recursively and get the number of e-mails inside?

The following script lists the top folders and its siblings,
unfortunately it is not dynamic enough to do it recursively.

Uwe


$outlook = new-object -com Outlook.Application
$namespace = $outlook.GetNamespace("MAPI")
$inbox = $outlook.Session.GetDefaultFolder(6)

# iterate through folders
foreach ($folder in $namespace.Folders) {
Write-Host $folder.name, $folder.count

foreach ($subfolder in $folder.Folders) {
Write-Host " >" $subfolder.name

}}


Shay Levy [MVP]

unread,
Sep 27, 2009, 8:32:46 AM9/27/09
to

Try with this, it lists all mailbox folders with each item's count:


function Get-MailboxFolder($folder)
{
"{0}: {1}" -f $folder.name, $folder.items.count

foreach ($f in $folder.folders)
{
Get-MailboxFolder $f
}
}


$ol = new-object -com Outlook.Application
$ns = $ol.GetNamespace("MAPI")
$mailbox = $ns.stores | where {$_.ExchangeStoreType -eq 0}
$mailbox.GetRootFolder().folders | foreach { Get-MailboxFolder $_}


---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
PowerShell Toolbar: http://tinyurl.com/PSToolbar

UZ> Hi,
UZ>
UZ> I would like to know the number of e-mails in each of my Outlook
UZ> folders but somehow I can't get the point: How can I go through the
UZ> list of folders recursively and get the number of e-mails inside?
UZ>
UZ> The following script lists the top folders and its siblings,
UZ> unfortunately it is not dynamic enough to do it recursively.
UZ>
UZ> Uwe
UZ>
UZ> $outlook = new-object -com Outlook.Application
UZ> $namespace = $outlook.GetNamespace("MAPI")
UZ> $inbox = $outlook.Session.GetDefaultFolder(6)
UZ> # iterate through folders
UZ> foreach ($folder in $namespace.Folders) {
UZ> Write-Host $folder.name, $folder.count
UZ> foreach ($subfolder in $folder.Folders) {
UZ> Write-Host " >" $subfolder.name
UZ> }}
UZ>


Uwe Ziegenhagen

unread,
Sep 27, 2009, 1:48:41 PM9/27/09
to
Thanks very much, unfortunately I received an error:

You cannot call a method on a null-valued expression.
At line:14 char:23
+ $mailbox.GetRootFolder <<<< ().folders | foreach { Get-MailboxFolder $_}
+ CategoryInfo : InvalidOperation: (GetRootFolder:String)
[], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

However I mixed Your code with mine successfully and got the following
running smoothly:


$outlook = new-object -com Outlook.Application

$namespace = $outlook.GetNamespace("MAPI")

$inbox = $outlook.Session.GetDefaultFolder(6)

function Get-MailboxFolder($folder)
{
"{0}: {1}" -f $folder.name, $folder.items.count

foreach ($f in $folder.folders) {
Get-MailboxFolder $f
}
}

# iterate through folders
foreach ($folder in $namespace.Folders) {
Get-MailboxFolder $folder
}

Best regards,


Uwe

Shay Levy [MVP] schrieb:

Uwe Ziegenhagen

unread,
Sep 27, 2009, 1:56:09 PM9/27/09
to
Just one more question, what does the following do?

"{0}: {1}" -f

I checked my books, could not find a description.

Uwe


Shay Levy [MVP] schrieb:
>

Uwe Ziegenhagen

unread,
Sep 27, 2009, 2:06:43 PM9/27/09
to
Uwe Ziegenhagen schrieb:

> Just one more question, what does the following do?
>
> "{0}: {1}" -f
>
> I checked my books, could not find a description.

<cut>

Found out myself:


"{0}: {1}" -f $folder.name, $folder.items.count


{0} first argument
: put a : here
{1} put the second argument here
$folder.name, $folder.items.count the two arguments


Uwe

Patrik Malina

unread,
Oct 6, 2009, 3:43:03 PM10/6/09
to
Hi.

This formating operator has been adopted from the .NET Framework. Check
here, for instance:
http://msdn.microsoft.com/en-us/library/txafckwd%28VS.71%29.aspx

P.


"Uwe Ziegenhagen" <news...@ziegenhagen.info> p??e v diskusn?m p??sp?vku
news:YOidna73C7I...@giganews.com...

0 new messages