Powershell: Outlook.Application access doesn’t work consistently


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. Disappointed smile

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s