Create folder in Outlook and move contents to it

Hi all

We have Outlook 2003, 2007 and 2010 and Exchange 2003 and the following

We need to create a folder in Outlook called "whatever" and move all the contents of the Sent Items folder into the folder called "whatever". If the Sent Items folder has a substructure like 2013, 2012 and 2011 this structure must be retained under the folder called "whatever"

Is this possible with powershell?

May 2nd, 2013 2:42pm

this is how i would create a new folder under my inbox folder

Add-Type -AssemblyName microsoft.office.interop.outlook
$olFolders = "Microsoft.Office.Interop.Outlook.OlDefaultFolders" -as [type]
$outlook = New-Object -ComObject outlook.application
$namespace = $Outlook.GetNameSpace("mapi")
$inbox = $namespace.getDefaultFolder($olFolders::olFolderInbox)
$newFolder = $inbox.Folders.Add("Test")
$MoveTarget = $inbox.Folders.item("Test")


then to move you can do something like
$i = $null
foreach($item in $inbox.items){
$i = $item | where {$_.subject -like "printer_686*"}
$i.Move($MoveTarget)
}
will give you your sent folder

$namespace.getDefaultFolder(5)
Free Windows Admin Tool Kit Click here and download it now
May 2nd, 2013 6:12pm

Thank you very much for taking the time to write something up for me. I have Outlook 2010 Pro on my pc and copied your code into PowerGui and run it from there. I then get the error below :

I then close Outlook and try again after which the Wizard comes up to create a new Outlook/Exchange Account. This i did cancell as it will create chaos if users must get that screen :). Any idea why the syntax does not work?

New-Object : Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80080005 Server execution failed (Exception from HRESULT: 0x80

080005 (CO_E_SERVER_EXEC_FAILURE)).

At line:259 char:22

+ $outlook = 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 line:260 char:35

+ $namespace = $Outlook.GetNameSpace <<<< ("mapi")

    + CategoryInfo          : InvalidOperation: (GetNameSpace:String) [], RuntimeException

    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.

At line:261 char:37

+ $inbox = $namespace.getDefaultFolder <<<< ($olFolders::olFolderInbox)

    + CategoryInfo          : InvalidOperation: (getDefaultFolder:String) [], RuntimeException

    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.

At line:262 char:32

+ $newFolder = $inbox.Folders.Add <<<< ("Test")

    + CategoryInfo          : InvalidOperation: (Add:String) [], RuntimeException

    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.

At line:263 char:34

+ $MoveTarget = $inbox.Folders.item <<<< ("Test")

    + CategoryInfo          : InvalidOperation: (item:String) [], RuntimeException

    + FullyQualifiedErrorId : InvokeMethodOnNull

May 3rd, 2013 8:34am

outlook needs to be running when this code is executated locally, is the mailbox not setup i dont know why wizard is poping up..

if you want an alternative server side cmdlets you need to look at http://help.outlook.com/140/dd575549.aspx

there are couple steps you need to do to set them up

Free Windows Admin Tool Kit Click here and download it now
May 6th, 2013 6:35pm

Thanks ImMax

I did run the script in PowerGui while Outlook was open and got the error in Red which i did paste earlier so that did not work

May 7th, 2013 10:08am

Also, the link you posted when I look at it, it looks as if it is for Exchange 2007 and up. We have Exchange 2003 SP2
Free Windows Admin Tool Kit Click here and download it now
May 7th, 2013 10:09am

I am having the same problem when trying to work off of the examples on this technet thread: http://blogs.technet.com/b/bill_long/archive/2010/04/06/accessing-the-information-store-from-powershell.aspx

I open with:

$outlook = new-object -com Outlook.Application

and get back:

new-object : Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80080005 Server execution failed (Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)).

At line:1 char:12

+ $outlook = new-object -com Outlook.Application

+            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : ResourceUnavailable: (:) [New-Object], COMException

    + FullyQualifiedErrorId : NoCOMClassIdentified,Microsoft.PowerShell.Commands.NewObjectCommand

Does anyone know what  we are doing wrong?


July 1st, 2013 7:40pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics