title: Powershell - Read and Move mails to archive folder
body: <p><strong>Problem:</strong></p>
  
  <p>Outlook:Cannot move read e-mail to Archive folder.</p>
  
  <p>Tried several options I found on the web but all to no avail... Anyone? :D</p>
  
  <p>I already removed the .move property as it was not working, so please don't fuss about that.</p>
  
  <p><em>Any help will be much appreciated!</em></p>
  
  <p>Code:</p>
  
  <pre><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&lt;Archive&gt;"))
  </code></pre>
  
  <p>}</p>