Outlook et Powershell - search doesn't yield results each time

Copper Contributor
Hi Team:
As I try to run this little Powershell script:





$outlook = new-object -com Outlook.Application;
$namespace = $outlook.GetNameSpace("MAPI");
$inputFolderObj=$namespace.Folders.Item('Email address removed').Folders.Item('temp')
$scope = $inputFolderObj.FolderPath
$filter = ""
$search = $outlook.AdvancedSearch("'$scope'", $filter, $True)





Count is '0' even though there is an email in the folder.

In debugging mode with breakpoint at the last line, I try to enter several times '$search', then '$search.Results', and finally I can see my email.


Do you know what is the problem underneath?


Outlook version 2202 build 14931.20132 Office

PSVersion 7.2.2

Ou PSVersion 5.1.22000.282




Savez-vous d'où vient le problème?

2 Replies
It does retrieve the correct number, but you have to wait a few seconds. It first returns 0 in my case too, but when I run $search.Results.Count again it shows 11 which was correct for the folder. Seems like the process is not ready when asking for the count after the search immediately?

Did it fix your issue if you put a start-sleep -seconds 10 after $search = $outlook.AdvancedSearch("'$scope'", $filter, $True)  ?