none
Moving older emails from Inbox folder to another folder using Powershell RRS feed

  • Question

  • Hi All,

    Many thanks in advance, as the title suggests I am trying to move older emails items from Inbox to another folder in the same mailbox. After research I got a script from the link: https://powershell.org/forums/topic/outlook-move-old-mail-out-of-inbox-to-folder/

    So, below is the script which used to do the job:

    $Date = [DateTime]::Now.AddDays(-150)
    
    $Last6Months =  $Date.tostring("MM/dd/yyyy")
    
    
    $o = New-Object -comobject outlook.application
    $n = $o.GetNamespace(“MAPI”)
    
    
    
    $Account = $n.Folders | ? { $_.Name -eq 'kathleen.nulty@hulldailymail.co.uk' };
    $Inbox = $Account.Folders | ? { $_.Name -match 'Inbox' };
    
    
    
    $Inbox.Items |
    Where-Object -FilterScript {
        $_.senton -ge "$Last6Months"  
    }  |
    ForEach-Object -Process {
        $psitem.Move('old emails')
    }

    The script is getting executed but no changes is happening on the mailbox, the emails remain in Inbox folder itself. Earlier I tried the same script with "$TargetFolder = $Inbox.Folders.Item('2016')" code line, but it was resulting an error:

    You cannot call a method on a null-valued expression.
    At C:\Users\username\Desktop\Scripts\Oldmailsmove.ps1:13 char:36
    + $TargetFolder = $Inbox.Folders.Item <<<< ('old emails')
        + CategoryInfo          : InvalidOperation: (Item:String) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull

    Please can anyone suggest what is wrong & where?

    Many Thanks,


    Niranjan

    Tuesday, September 12, 2017 8:04 AM

Answers

  • This is how we have to do this in Outlook.

    $ol = New-Object -comobject outlook.application
    $ns = $ol.GetNamespace('MAPI')
    $store = $ns.Stores['kathleen.nulty@hulldailymail.co.uk']
    $inbox = $store.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox)
    $root = $store.GetRootFolder()
    $targetFolder = $root.Folders['old emails']
    $inbox.Items | 
    	Where-Object{$_.SentOn -ge [datetime]::Today.AddDays(-150)} |
    	ForEach-Object{$_.Move($targetFolder)}


    \_(ツ)_/


    • Edited by jrv Tuesday, September 12, 2017 10:26 AM
    • Proposed as answer by jagan1818 Tuesday, September 12, 2017 3:02 PM
    • Marked as answer by Richard MuellerMVP, Moderator Sunday, September 24, 2017 5:53 PM
    Tuesday, September 12, 2017 10:25 AM

All replies

  • First remove the quotes and use a date and not a string.

    $Inbox.Items | Where-Object{$_.senton -ge [DateTime]::Now.AddDays(-150) } 

    Date objects are not strings.

    'old emails' is not a folder so you have a null object.


    \_(ツ)_/

    Tuesday, September 12, 2017 8:14 AM
  • Hi Jrv,

    Thanks for the reply, the folder 'Old emails' is located at the root & not under Inbox.

    Many Thanks,


    Niranjan

    Tuesday, September 12, 2017 9:45 AM
  • The error does not match the code:

    $TargetFolder = $Inbox.Folders.Item <<<< ('old emails')

    That line does not match the code you posted.


    \_(ツ)_/

    Tuesday, September 12, 2017 10:09 AM
  • This is how we have to do this in Outlook.

    $ol = New-Object -comobject outlook.application
    $ns = $ol.GetNamespace('MAPI')
    $store = $ns.Stores['kathleen.nulty@hulldailymail.co.uk']
    $inbox = $store.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox)
    $root = $store.GetRootFolder()
    $targetFolder = $root.Folders['old emails']
    $inbox.Items | 
    	Where-Object{$_.SentOn -ge [datetime]::Today.AddDays(-150)} |
    	ForEach-Object{$_.Move($targetFolder)}


    \_(ツ)_/


    • Edited by jrv Tuesday, September 12, 2017 10:26 AM
    • Proposed as answer by jagan1818 Tuesday, September 12, 2017 3:02 PM
    • Marked as answer by Richard MuellerMVP, Moderator Sunday, September 24, 2017 5:53 PM
    Tuesday, September 12, 2017 10:25 AM
  • That was a typo from my side while posting it here. It is actually:

    $TargetFolder = $Inbox.Folders.Item('Old emails')

    Also, my apologies for posting the email address as well.


    Niranjan

    Tuesday, September 12, 2017 3:25 PM
  • Hey Jrv,

    I ran the above script opening Outlook 2010 & below is the error what I got:

    New-Object : Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80080005.
    At C:\Users\user\Desktop\oldmail.ps1:1 char:17
    + $ol = New-Object <<<<  -comobject outlook.application
        + CategoryInfo          : ResourceUnavailable: (:) [New-Object], COMException
        + FullyQualifiedErrorId : NoCOMClassIdentified,Microsoft.PowerShell.Commands.NewObjectCommand
     
    You cannot call a method on a null-valued expression.
    At C:\Users\user\Desktop\oldmail.ps1:2 char:23
    + $ns = $ol.GetNamespace <<<< ('MAPI')
        + CategoryInfo          : InvalidOperation: (GetNamespace:String) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull
     
    Cannot index into a null array.
    At C:\Users\user\Desktop\oldmail.ps1:3 char:21
    + $store = $ns.Stores[ <<<< 'kathleen.nulty@hulldailymail.co.uk']
        + CategoryInfo          : InvalidOperation: (kathleen.nulty@hulldailymail.co.uk:String) [], RuntimeException
        + FullyQualifiedErrorId : NullArray
     
    You cannot call a method on a null-valued expression.
    At C:\Users\user\Desktop\oldmail.ps1:4 char:33
    + $inbox = $store.GetDefaultFolder <<<< ([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox)
        + CategoryInfo          : InvalidOperation: (GetDefaultFolder:String) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull
     
    You cannot call a method on a null-valued expression.
    At C:\Users\user\Desktop\oldmail.ps1:5 char:29
    + $root = $store.GetRootFolder <<<< ()
        + CategoryInfo          : InvalidOperation: (GetRootFolder:String) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull
     
    Cannot index into a null array.
    At C:\Users\user\Desktop\oldmail.ps1:6 char:31
    + $targetFolder = $root.Folders[ <<<< 'old emails']
        + CategoryInfo          : InvalidOperation: (old emails:String) [], RuntimeException
        + FullyQualifiedErrorId : NullArray


    Niranjan

    Thursday, September 14, 2017 8:59 AM
  • Outlook 2010 is no longer supported.  You may have many issues with it. YOU may also have issues running OL2010 on a 64 bit system.  Try running a 32 bit version of PowerShell.


    \_(ツ)_/

    Thursday, September 14, 2017 9:08 AM
  • Thanks for the reply JRV,

    Below is the exception what I am getting now:

    Exception calling "Move" with "1" argument(s): "Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"
    At C:\Users\user\Desktop\oldmail.ps1:9 char:24
    +     ForEach-Object{$_.Move <<<< ($targetFolder)}
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : ComMethodTargetInvocation

    Many Thanks,


    Niranjan

    Thursday, September 14, 2017 11:22 AM
  • I got the above exception when I was trying to execute the below script:

    $ol = New-Object -comobject outlook.application
    $ns = $ol.GetNamespace('MAPI')
    $store = $ns.Stores['kathleen.nulty@hulldailymail.co.uk']
    $inbox = $store.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox)
    $root = $store.GetRootFolder()
    $targetFolder = $root.Folders['old emails']
    $inbox.Items | 
    	Where-Object{$_.SentOn -ge [datetime]::Today.AddDays(-150)} |
    	ForEach-Object{$_.Move($targetFolder)}


    Niranjan

    Thursday, September 14, 2017 11:24 AM
  • Your target folder doe not exist.

    As noted above, O2010 is no linger a supported product and apparently doesn't support the "Move" method.  It is also possible that the target folder is not available.

    I have no way of testing on 2010 and it works on Outlook 2013 and later.


    \_(ツ)_/

    Thursday, September 14, 2017 3:13 PM
  • Hi Jrv,

    The folder does exist, & I tried on Outlook 2013 as well, but still got this error:

    New-Object : Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80080005.
    At C:\Users\user\Desktop\oldemails.ps1:1 char:17
    + $ol = New-Object <<<<  -comobject outlook.application
        + CategoryInfo          : ResourceUnavailable: (:) [New-Object], COMException
        + FullyQualifiedErrorId : NoCOMClassIdentified,Microsoft.PowerShell.Commands.NewObjectCommand

    You cannot call a method on a null-valued expression.
    At C:\Users\user\Desktop\oldemails.ps1:2 char:23
    + $ns = $ol.GetNamespace <<<< ('MAPI')
        + CategoryInfo          : InvalidOperation: (GetNamespace:String) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull

    Cannot index into a null array.
    At C:\Users\user\Desktop\oldemails.ps1:3 char:21
    + $store = $ns.Stores[ <<<< 'kathleen.nulty@hulldailymail.co.uk']
        + CategoryInfo          : InvalidOperation: (kathleen.nulty@hulldailymail.co.uk:String) [], RuntimeException
        + FullyQualifiedErrorId : NullArray

    You cannot call a method on a null-valued expression.
    At C:\Users\kat.nulty\Desktop\oldemails.ps1:4 char:33
    + $inbox = $store.GetDefaultFolder <<<< ([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox)
        + CategoryInfo          : InvalidOperation: (GetDefaultFolder:String) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull

    You cannot call a method on a null-valued expression.
    At C:\Users\user\Desktop\oldemails.ps1:5 char:29
    + $root = $store.GetRootFolder <<<< ()
        + CategoryInfo          : InvalidOperation: (GetRootFolder:String) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull

    Cannot index into a null array.
    At C:\Users\user\Desktop\oldemails.ps1:6 char:31
    + $targetFolder = $root.Folders[ <<<< 'old emails']
        + CategoryInfo          : InvalidOperation: (old emails:String) [], RuntimeException

        + FullyQualifiedErrorId : NullArray    

    Many Thanks,


    Niranjan

    Tuesday, September 19, 2017 2:33 PM
  • I suggest that you need to fix your system.  If this line cannot be executed then something is very broken:

    $ol = New-Object  -comobject outlook.application


    \_(ツ)_/

    Tuesday, September 19, 2017 4:53 PM