-1

I'm stucked, i'm able to connect a pst file in Outlook, I would like to loop through all folders / search a certain email with a certain subject and then, delete it. I've searched through the web but don't know how to start, can someone explain me plz ? I'm lost with $namespace.folders.item.

Clear-host
Add-Type -Assembly "Microsoft.Office.Interop.Outlook"
$Outlook = New-Object -ComObject Outlook.Application
$Namespace = $Outlook.GetNameSpace("MAPI")
$folders = "C:\Tools\Archive.pst"
$namespace.AddStore($folder)

I'm thinkin' about this but I know i'm wrong :

$email = $folders.Folders.items | Where-Object {$_.Subject -like "*alert*"} OR
Foreach ($Folder in $SubFolder.Folders)
{
    $ItemsToDelete = $Folder.Items | Where-Object -Property subject -like "*alert*"
    foreach ($item in $ItemsToDelete)
    {
        $item.Delete()
    }
}

(all articles at Microsoft's are related to VB which I don't know and i'm starting Powershell : https://learn.microsoft.com/en-us/office/vba/api/outlook.namespace.addstore)

Thx for your advices.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
Pax212fr
  • 9
  • 2

1 Answers1

0

After adding a new store in Outlook you can find it in the Stores collection which you may get by using the Stores property of the Namespace class. Use the Stores and Store objects to enumerate all folders and search folders on all stores in the current session. The following VBA sample code shows how you can use these objects to iterate over all stores and folders in Outlook:

Sub EnumerateFoldersInStores() 
 Dim colStores As Outlook.Stores 
 Dim oStore As Outlook.Store 
 Dim oRoot As Outlook.Folder 
 
 On Error Resume Next 

 Set colStores = Application.Session.Stores 
 For Each oStore In colStores 
   Set oRoot = oStore.GetRootFolder 
   Debug.Print (oRoot.FolderPath) 
   EnumerateFolders oRoot 
 Next 
End Sub 
 
Private Sub EnumerateFolders(ByVal oFolder As Outlook.Folder) 
 Dim folders As Outlook.folders 
 Dim Folder As Outlook.Folder 
 Dim foldercount As Integer 
 
 On Error Resume Next 
 
 Set folders = oFolder.folders 
 foldercount = folders.Count 
 'Check if there are any folders below oFolder 
 If foldercount Then 
   For Each Folder In folders 
     Debug.Print (Folder.FolderPath) 
     EnumerateFolders Folder 
   Next 
 End If 
End Sub

Note, the Outlook object model is common for all programming languages, so you may find the sequence of property and methods calls in the sample shown above.

To find items that correspond to your conditions you need to use the Find/FindNext or Restrict methods of the Items class. In that case you will get only items that correspond to the search criteria and then you can iterate over them only. Read more about these methods in the articles I wrote for the technical blog:

If you need to find items in multiple folders I'd recommend choosing the AdvancedSearch method of the Application class instead. The key benefits of using the AdvancedSearch method in Outlook are:

  • The search is performed in another thread. You don’t need to run another thread manually since the AdvancedSearch method runs it automatically in the background.
  • Possibility to search for any item types: mail, appointment, calendar, notes etc. in any location, i.e. beyond the scope of a certain folder. The Restrict and Find/FindNext methods can be applied to a particular Items collection (see the Items property of the Folder class in Outlook).
  • Full support for DASL queries (custom properties can be used for searching too). To improve the search performance, Instant Search keywords can be used if Instant Search is enabled for the store (see the IsInstantSearchEnabled property of the Store class).
  • You can stop the search process at any moment using the Stop method of the Search class.

See Advanced search in Outlook programmatically: C#, VB.NET for more information.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Thanks a lot for your explanations I drowned ;( I don't understand the logic neither in Posh nor VB which I don't know... you add the store and then how do you query it ? I've seen that you need a GetRootFolder and count folder to access the items ? – Pax212fr Oct 02 '22 at 19:08
  • To find the just added store in Outlook you need to use the [Stores](https://learn.microsoft.com/en-us/office/vba/api/outlook.namespace.stores) property of the `Namespace` class. Note, the Outlook object model is common for all programming languages, so you can find the sequence of property and method calls in the samples I posted above. – Eugene Astafiev Oct 03 '22 at 12:24