Jeff Hicks' Off-the-Wall Challenge: Using PowerShell to read emails

- select the contributor at the end of the page -
Reading emails the old-fashioned way is soooo 2012. Why go through the trouble of opening Outlook and reading each one yourself when you can write PowerShell code to do that for you?

To take part in Jeff Hicks' Off-the-Wall PowerShell Challenge, which kicked off on the TrainSignal blog yesterday, Nick Sinclair submitted code that does just that.

"Inspired by your use of SAPI.SPVoice, I just wrote this to check the last 10 messages received in Outlook, and read the unread messages to me," said Nick in his entry. He tested the code on Windows 8 with PowerShell 3.0 and Outlook 2010.

Want to try it yourself? See Nick's code below, and don't forget you still  have six days to enter the challenge. One lucky winner will get a one-year subscription to TrainSignal, a $468 value! Find out more about the contest here.

$olFolderInbox = 6 $outlook = new-object -com outlook.application; $ns = $outlook.GetNameSpace("MAPI"); $inbox = $ns.GetDefaultFolder($olFolderInbox) #checks 10 newest messages $inbox.items | select -first 10 | foreach { if($_.unread -eq $True) { $mBody = $_.body #Splits the line before any previous replies are loaded $mBodySplit = $mBody -split "From:" #Assigns only the first message in the chain $mBodyLeft = $mbodySplit[0] #build a string using the –f operator $q = "From: " + $_.SenderName + ("`n") + " Message: " + $mBodyLeft #create the COM object and invoke the Speak() method (New-Object -ComObject SAPI.SPVoice).Speak($q) | Out-Null } }

Get our content first. In your inbox.

Loading form...

If this message remains, it may be due to cookies being disabled or to an ad blocker.


Dana Gagnon

is the Director of Branded Content at Pluralsight. After working for years in Chicago media, she joined the team in 2012 to continue bringing quality news, tips and more to Pluralsight's audience. Find her @ChicagoDana or on Google+.