PoshCode Logo PowerShell Code Repository

Powershell Only Outlook by chris seiter 3 years ago
View followups from regin | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/2695"></script>download | new post

My first contribution. I am crazily thinking of doing a “Powershell Only” day. The first task is to figure out how to manipulate Outlook through PowerShell. The submitted script hits my Outlook inbox and goes through the inbox and each subfolder and retrieves the unread emails from it. It then goes through my task list and gets all the incomplete tasks. This was my first time using a status bar and definitely the first for making anything outside the scripting games public. I’d hate to get finished with the Outlook “module” and find out I could have saved myself a lot of time, so I through the script as it is now on the mercy of the court. Proceed with your red pens…

  1. #connect to outlook
  2. $GetOutlook = New-Object -com "Outlook.Application";
  3. $olName = $GetOutlook.GetNamespace("MAPI")
  4. $olxEmailFolder = $olName.GetDefaultFolder(‘olFolderInbox’)
  5. $olxEmailFolder.Name
  6. $olxEmailItem = $olxemailFolder.items
  7. #show unread emails in inbox
  8. $olxEmailItem | ?{$_.Unread -eq $True} | select SentOn, SenderName, Subject, Body | Format-Table -auto
  9. #go through each subfolder and get name
  10. $SubFolders = $olxEmailFolder.Folders
  11. ForEach($Folder in $SubFolders)
  12. {
  13.         $Folder.Name
  14.         $SubfolderItem = $Folder.Items
  15.         $EmailCount = 1
  16. #create status bar for each subfolder
  17.         ForEach($Email in $SubfolderItem)
  18.         {
  19.                 Do
  20.                 {
  21.                         Write-Progress -Activity "Checking folder" -status $Folder.Name -
  22.  
  23. PercentComplete ($EmailCount/$Folder.Items.Count*100)
  24.                         $EmailCount++
  25.                 }
  26. #show unread emails from subfolders
  27.                 While($EmailCount -le $Folders.Item.Count)
  28.         $Email | ?{$_.Unread -eq $True} | select SentOn, SenderName, Subject, Body | Format-Table -
  29.  
  30. Auto
  31.         }
  32. }
  33. #connect to tasks
  34. $olxTasksFolder = $olName.GetDefaultFolder(‘olFolderTasks’)
  35. $olxTaskItems = $olxTasksFolder.items
  36. $TaskCount = 1
  37. #create task array
  38. $TaskList = @()
  39. ForEach($TaskItem in $olxTaskItems)
  40. {
  41. #create status bar for tasks
  42.         Do
  43.         {
  44.                 Write-Progress -Activity "Checking" -status "Tasks" -PercentComplete ($Taskcount/
  45.  
  46. $olxTasksFolder.Items.count*100)
  47.                 $TaskCount++
  48.         }
  49. #add each incomplete tash to array
  50.         While($TaskCount -le $olxTaskFolder.Items.Count)
  51.         If($TaskItem.Complete -eq $False)
  52.         {
  53.         $TaskList+=New-Object -TypeName PSObject -Property @{
  54.         Subject=$TaskItem.Subject
  55.         DateCreated=$TaskItem.CreationTime
  56.         Percent=$TaskItem.PercentComplete
  57.         Due=$TaskItem.DueDate
  58.         }}
  59. }
  60. #show task array to screen
  61. $TaskList | Sort DueDate -descending | Format-Table -Auto

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me