Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to query my inbox with powershell recursivly for all "undelivered email returner"...

I have problems with the recursive part...

This is what I got:

$outlook = new-object -com Outlook.Application
$ns = $olApp.GetNamespace("MAPI")
$mb = $namespace.Folders | ?{$ -match "mailbox"}   
$folder1 = $mb.Folders | ?{$ -match "folder1"}   
$folder1.Folders | %{$}  

$folder1.items | foreach {
if($_.subject -match "undelivered") {...}

This is what I got ... but I have some sort of problem with the recursive part in scripting..

any help would be greate...


share|improve this question

Here's a short script that should help you out. It walks through all folders in a mailbox and outputs their path. You can update the work done in the recursive section to check the items in each folder as it passes through them.

$outlook = New-Object -Com Outlook.Application
$mapi = $outlook.GetNamespace('MAPI')
$mailboxRoot = $mapi.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox).Parent
$walkFolderScriptBlock = {
    foreach ($item in $currentFolder.Folders) {
    	& $walkFolderScriptBlock $item
& $walkFolderScriptBlock $mailboxRoot
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.