Speed up conversion .msg files to .doc using powershell

We have a project where we need to move files from a windows server to google workspace.
On the server are many .msg files.
I would like to convert them to .doc for instance.
Currently I have the following code :

$outlook = New-Object -ComObject Outlook.Application
$word = New-Object -ComObject Word.Application

        $msg = $outlook.CreateItemFromTemplate($msgFullName)
        $msg.Attachments | ForEach-Object {
            $attachment = $_.SaveAsFile((Join-Path (Split-Path $docFullName) $_.FileName))
           # $attachment.Dispose()
        }

        $msg.SaveAs($docFullName, 4)

But this is very slow . Any suggestions to do this faster?

Rob,
Goedeavond and Welcome to the forum. :wave:t3:

Please … when you post code, sample data, console output or error messages format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

How many?

What part exactly is slow? Did you measure it? How do you actually populate the variables $msgFullName and $docFullName?

On a server it converts average 13 .msg per minute. On a local PC it is a lot faster (120 a minute) .
Could it be because i read a lot of subdirectries ?

The full code looks like this (Any comments welcome)

Param (
    [ Parameter(Mandatory=$true)]
    [string]$folderpath,   
    [ Parameter(Mandatory=$true)]
    [string]$bijwerken
    ) 


$textfile = "C:\powershell\Conv_Msg_Doc.txt"

$outlook = New-Object -ComObject Outlook.Application
$word = New-Object -ComObject Word.Application

$DtTd1 = (Get-Date).toString("MM-dd-yyyy_HH:mm:ss")
Add-Content $textfile "Start Time is : ${DtTd1}"
$tellen = 0


Get-ChildItem -Path $folderPath -Filter *.msg? -Recurse | ForEach-Object  {

    $msgFullName = $_.FullName

    if ($bijwerken -eq "J") 
        {
            $docFullName = $msgFullName -replace '\.msg$', '.doc'

            $msg = $outlook.CreateItemFromTemplate($msgFullName)
            $msg.Attachments | ForEach-Object {
                $attachment = $_.SaveAsFile((Join-Path (Split-Path $docFullName) $_.FileName))
               # $attachment.Dispose()
            }

            $msg.SaveAs($docFullName, 4)

        }

    $tellen = $tellen + 1
    Add-Content $textfile $msgFullName

 }

$DtTd2 = (Get-Date).toString("MM-dd-yyyy_HH:mm:ss")
Add-Content $textfile "End Time is : ${DtTd2}"
Add-Content $textfile "nr files converted is : ${tellen} "

When you say "on a server … " are you talking about to run the code on the server or via network (UNC path) against a server?

Hi, I really mean on a server. (Using rdp)
Any comments on the code itself ?

Hmmm … actually not yet … despite you declaire but you don’t use the variables $word and $attachment and the quite costly logging the code seems to be just fine. I’d probably remove all the logging if I’m looking for speed (file system operations are usually quite slow - if you need logging collect the information in a varialbe and write them all together at once at the end) but appart from that … :man_shrugging:t3: :wink: And since you say …

… the code itself does not seem to be the issue here. If you have such a big difference between running it on a client and on a server what’s the difference between them? I assume the files you’re about to process with the script are local to the computer the code runs on, right?

If there is a bottleneck in your code you should measure where it is. Otherwise you may waste a lot of energy optimizing code for a benefit not worth it. :man_shrugging:t3:

Hi Olaf,
Thanks for your suggestion to do the logging in a variable.
That gives a 90% improvement!.
Apparently the server disks are slow.
Regarda, Rob

Wow … if they are that slow you may seriously considering an upgrade to a more up to date hardware. :wink:

And just out of curiousity - does it run faster on the client as well?

Hi, Yes, on a laptop it is also faster, but not as much difference , roughly 20% .
With regards to the server hardware, it is not my server. :slight_smile: