1

I'm exporting Outlook contacts to a .csv file using Powershell. Here are the relevant parts of the PS script:

$Outlook = New-Object -comobject Outlook.Application
$Contacts = $Outlook.session.GetDefaultFolder(10).items
$count = $Contacts.Count
$PSDefaultParameterValues['Out-File:Encoding'] = "utf8"

$Contacts | Select * | Export-Csv -Encoding UTF8 -NoTypeInformation "$outfile"

This is on a Windows 10 machine with Office 365. The contacts are in a local .pst file (there is no Exchange server).

It takes 15 minutes to export around 3100 contacts!

Is there a way to make this much faster? Or some other way to export these contacts?

Update:

I tried to use a "Table" object as suggested by Eugene Astafiev below, but without a filter and with Powershell instead of VB. But I cannot get any useful output with what I tried.

This for example:

$Folder = $Outlook.session.GetDefaultFolder(10)
$Table = $Folder.GetTable()
while ( $row = $Table.GetNextRow() ) { $row }

doesn't print anything useful from the contacts. It only prints

Application                                       Class Session            Parent
-----------                                       ----- -------            ------
Microsoft.Office.Interop.Outlook.ApplicationClass   121 System.__ComObject System.__ComObject
Microsoft.Office.Interop.Outlook.ApplicationClass   121 System.__ComObject System.__ComObject
... etc.
mivk
  • 13,452
  • 5
  • 76
  • 69
  • There are no PowerShell programming bottlenecks in your example, in other words it looks to me as a pure **outlook** limitation issue. – iRon Feb 24 '23 at 07:31

1 Answers1

0

You may consider using the Table object which represents a set of item data from a Folder or Search object, with items as rows of the table and properties as columns of the table.

The Table represents a read-only dynamic rowset of data in a Folder object. Use Folder.GetTable to obtain a Table object that represents a set of items in a folder or search folder. If the Table object is obtained from Folder.GetTable, you can further specify a filter (in Table.Restrict) to obtain a subset of the items in the folder. If you don't specify any filter, you'll obtain all the items in the folder.

By default, each item in the returned Table contains only a default subset of its properties. You can regard each row of a Table as an item in the folder, each column as a property of the item, and the Table as an in-memory lightweight rowset that allows fast enumeration and filtering of items in the folder. Although additions and deletions of the underlying folder are reflected by the rows in the Table, the Table does not support any events for adding, changing, and removing of rows. For more information on default columns in a Table, see Default Properties Displayed in a Table Object.

The following VBA sample code shows how to use the GetTable method of the Folder class when dealing with the Outlook object model:

Sub DemoTable()  
    'Declarations  
    Dim Filter As String  
    Dim oRow As Outlook.Row  
    Dim oTable As Outlook.Table  
    Dim oFolder As Outlook.Folder  
  
    'Get a Folder object for the Inbox  
    Set oFolder = Application.Session.GetDefaultFolder(olFolderInbox)  
  
    'Define Filter to obtain items last modified after January 1, 2022  
    Filter = "[LastModificationTime] > '1/1/2022'"  
    'Restrict with Filter  
    Set oTable = oFolder.GetTable(Filter)  
  
    'Enumerate the table using test for EndOfTable  
    Do Until (oTable.EndOfTable)  
        Set oRow = oTable.GetNextRow()  
        Debug.Print (oRow("Subject"))  
        Debug.Print (oRow("LastModificationTime"))  
    Loop  
End Sub
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • I tried to use Table, but without success. See the updated question. – mivk Feb 24 '23 at 17:34
  • You can add the require properties to the table, see the `Table.Columns.Add` method for more information. You can find [default](https://learn.microsoft.com/en-us/office/vba/outlook/how-to/search-and-filter/default-properties-displayed-in-a-table-object#columns-for-the-contacts-folder) property set for the contacts folder in MSDN. – Eugene Astafiev Feb 24 '23 at 17:39