I was running Outlook 2010 and running an Powershell script to access emails by sub folders and count them for a weekly report. The problem was script seems to failing during testing. Sometime I get list of emails displaying, other times it was nothing.
I started my script with
$outlook = new-object -com Outlook.Application
$session = $outlook.Session
When I am done with subfolders and mail counting, my script quits right away. Seems simple, but script didn’t work correctly most of the times.
The issue was I wasn’t closing the Outlook object (outlook.application) in the script. Outlook was hanging around the memory and script was failing to open the outlook object while other object was in memory already.
Adding the following line to close the Outlook Object worked the wonders.
$outlook.Quit()
Ok I pledge I will clean up the memory in my scripts.