Create an Outlook Archive with Powershell

2.2k Views Asked by At

I have to create archive (.pst file) for mailbox in Outlook. I use an exchange server 2016. If i do the "microsoft way" (https://support.microsoft.com/en-us/office/archive-items-manually-ecf54f37-14d7-4ee3-a830-46a5c33274f6) i always find mails that aren't transfert to the archive. Cached Exchange Mode are turn off.

I wish to find a way with powershell to access Outlook and the mail box. I want to copy all folders and subfolders name, their hierarchy, to create them on my archive.pst

Then, i want to find all mail prior to a specific date, and copy each mail on the right folder.

Thank to the post here : Powershell - Read and Move mails to archive folder the function "Get-OutlookInBox" could give me mails, but only it seems on Inbox, not the others folders.

Can anyone tell me how to do this ?

Thank in advance

2

There are 2 best solutions below

2
Eugene Astafiev On

Posts you are referring to use PowerShell scripts that automate Outlook. The OOM has access to the local data only.

Instead, you can use EWS, see Explore the EWS Managed API, EWS, and web services in Exchange for more information. But EWS is discontinued, so now Graph API is the best and valid approach for that, read more about that in the Outlook mail API overview article.

Then, i want to find all mail prior to a specific date, and copy each mail on the right folder.

Here you are free to use a powershell script which searches for items that correspond to your conditions. The Find/FindNext or Restrict methods can help with such tasks in Outlook:

3
mikael boyer Pro On

I try to use that function :

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-Object Subject, UnRead, ReceivedTime, SenderName #* # Subject, UnRead, ReceivedTime, SenderName 
}

But it only analyse the Inbox folder, not the others. And i don't have an object refering to the folder

Maybe i don't use the right code ?