Royston
Royston

Reputation: 598

how to delete outlook inbox subfolder contents using powershell

there must be a way of doing this using the MAPI.

I have

    $ns = $outlook.GetNameSpace("MAPI"); 
    $inbox = $ns.GetDefaultFolder($olFolderInbox);
    $inbox.Folders `
| ? name -eq Subfolder1 ` 
| % Items

There must be a way of deleting the contents of this subfolder folder after the full script runs, so the subfolder is clear for the next time the script runs to process only the items as they come in new. (so as not to have processed files reprocessing). Any ideas?

Upvotes: 1

Views: 2567

Answers (1)

Avshalom
Avshalom

Reputation: 8889

Simply Use the Delete Method:

$ns = $outlook.GetNameSpace("MAPI"); 
$inbox = $ns.GetDefaultFolder('olFolderInbox')
$SubFolders = $inbox.Folders

To Delete all the Subfolders with the contents:

$SubFolders | % {$_.Delete()}

To Delete only The Contents

foreach ($SubFolder in $SubFolders)
{
    While ($Subfolder.Items.Count -ne 0)
    {
    $SubFolder.Items | % {$_.delete()}
    }
}
  • Note: for some reason, sometimes it's not deleting all the items, This can be solved in a simple While Loop, like in the example

Upvotes: 1

Related Questions