Search code examples
emailpowershelloutlook

Powershell - Read and Move mails to archive folder


Problem:

Outlook:Cannot move read e-mail to Archive folder.

Tried several options I found on the web but all to no avail... Anyone? :D

I already removed the .move property as it was not working, so please don't fuss about that.

Any help will be much appreciated!

Code:

start Outlook
Function Get-OutlookInBox 
    { 
    Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null 
    $olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]  
    $outlook = new-object -comobject outlook.application 

        $namespace = $outlook.GetNameSpace("MAPI") 
        $folder = $namespace.getDefaultFolder($olFolders::olFolderInBox) 
            $folder.items |  
            Select -Property Subject, ReceivedTime, Importance, SenderName, body 
    } #end function Get-OutlookInbox
    ###################################################################################

    cls

try {
    $switches = get-outlookinbox | where subject -eq "Ip was blocked"
        $e = $switches.body
    $e = $e -replace 'Blocked IP:| for agent| |:\d{1,2}'
                    }
    catch {
          $switches = "Fail"
          }

    $f = $e |select -Unique |sort


    ni $File -type file
        $f | ac $File
            (gc $File) | ? {$_.trim() -ne "" } | sc $File
            $IPCount =  (gc $File)
            $IPCount =  $IPCount.count

    $index=0;  

    #Mark mails as read and delete.
    function display( [string]$subject, [string]$color , [string]$out)  {

    # REQUIRED LENGTH OF STRING
    $len = 20

    # STRINGS THAT ARE LONGER WILL BE CUT DOWN,
    # STRINGS THAT ARE TO SHORT WILL BE MADE LONGER
    if ( $subject.length -lt 20 ){
        $toadd=20-$subject.length;
        for ( $i=0; $i -lt $toadd; $i++ ){
            $subject=$subject+" ";
        }
        $len = $subject.length
    }
    else { $len = 20 }

    $index=$index+1
    Write -ForegroundColor $color -nonewline " |" ((($subject).ToString()).Substring(0,$len)).ToUpper()
}
$outlook = new-object -comobject outlook.application

#Define folders
$namespace = $outlook.GetNameSpace("MAPI")
$pst = $namespace.Stores
$pstRoot = $pst.GetRootFolder()
$pstFolders = $pstRoot.Folders
$Archive = $namespace.Folders | ?{$_.name -match "Archive"}
$DefaultFolder = $namespace.GetDefaultFolder(6)
$InboxFolders = $DefaultFolder.Folders
$DeletedItems = $namespace.GetDefaultFolder(3)
$Emails = $DefaultFolder.Items

#For($i=($emails.count-1);$i -ge 0;$i--){
 #   $($emails)[$i].Unread = $false
  #  $($emails)[$i].delete()
#}
For($i=($emails.count-1);$i -ge 0;$i--){
$($emails)[$i].Unread = $false
$($emails)[$i].move($Archive.Folders.Item("Inbox<Archive>"))

}


Solution

  • That's a different way to get the folders than I'm used to using. This should do it for you though:

    $Archive = $namespace.Folders | ?{$_.name -match "Archive"}
    

    And then in your For loop if you want to move it to your Inbox in the Archives you can do this:

    For($i=($emails.count-1);$i -ge 0;$i--){
        $($emails)[$i].Unread = $false
        $($emails)[$i].move($Archive.Folders.Item("Inbox"))
    }
    

    If you want a subfolder you add more .Folders.Item("<Subfolder Name>) on after ("Inbox")