PoshCode Archive  Artifact Content

Artifact 84bde87cac9cde5731808ab2a70162122a6e74986e3900b697c8f87b4625518c:

  • File Remove-Duplicate-Notes.ps1 — part of check-in [4570bc05ea] at 2018-06-10 13:25:53 on branch trunk — Remove duplicate Notes from Outlook. I had many duplicate notes after a bad sync with my smartphone. This removed them. (user: MichaelJ size: 1477)

# encoding: ascii
# api: powershell
# title: Remove Duplicate Notes
# description: Remove duplicate Notes from Outlook.  I had many duplicate notes after a bad sync with my smartphone.  This removed them.
# version: 0.1
# author: MichaelJ
# license: CC0
# x-poshcode-id: 3451
# x-archived: 2012-06-18T09:49:21
# x-published: 2012-06-10T19:50:00
#
#
# remove outlook duplicate notes and put the removed note in a file

$outlook = new-object -comobject outlook.application

$session = $outlook.session
$session.logon()

$olFoldersEnum = "Microsoft.Office.Interop.Outlook.OlDefaultFolders" -as [type]
$notes = $session.getdefaultfolder($olFoldersEnum::olFolderNotes).items

Write-Host "Number of notes:" $notes.count

$noteStringModifiedDateMap = @{}
$notesToDelete = @()

Foreach ($e in $notes)
    { 
        $noteText = $e.Body
        $noteModified = $e.LastModificationTime
        if ($noteStringModifiedDateMap.ContainsKey($noteText))
        {
            $mapElement = $noteStringModifiedDateMap.Get_Item($noteText)
            if ($mapElement.LastModificationTime -gt $noteModified) # delete the new one
                { $notesToDelete += $mapElement } 
            else
                { $notesToDelete += $e }
        }
        else
        { $noteStringModifiedDateMap.Add($noteText, $e) }
     }
     
$notesToDelete | Export-Csv "C:\toDelete.csv"

foreach ($e in $notesToDelete)
{ $e.Delete() }

Write-Host "done!"