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
}}
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>
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:
"{0}: {1}" -f
I checked my books, could not find a description.
Uwe
Shay Levy [MVP] schrieb:
>
<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
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...