Trying to Convert .MSGs in multiple sub folders into .PST

19 Views Asked by At

Good Morning,

I'm trying to use PowerShell to convert MSGs to a PST

Was using DocuWare to archive emails but now trying to use Barracuda. DocuWare exported the emails as .MSGs and Barracuda does not import .MSGs.

I have main Folder with 256 Folders, each of those 256 folders has 256 subfolders with another 256 subfolders that then has 2 .msg files in them.

Tried using some software to do this like 'CubexSoft MSG Export' but it takes about a week to go through just one of main folders.

I found this script to try using powershell script below, but i'm getting errors. Pasted the scrpt and the errors below.

If anyone has any suggestions, I would greatly appreciate it.

  • SCRIPT:

Load Outlook COM objects

Add-Type -AssemblyName "Microsoft.Office.Interop.Outlook"

Specify the root folder containing subfolders with .msg files

$rootFolder = "C:\Email\000"

Specify the output .pst file

$pstFile = "C:\Email\Export\000.pst"

Create a new Outlook Application

$outlook = New-Object -ComObject Outlook.Application

Function to recursively search through folders and convert .msg files to .pst

function ConvertToPST { param ( [string]$folderPath )

# Get a list of .msg files in the current folder
$msgFiles = Get-ChildItem -Path $folderPath -Filter *.msg

# Loop through each .msg file
foreach ($msgFile in $msgFiles) {
    # Open the .msg file
    $mailItem = $outlook.Session.OpenSharedItem($msgFile.FullName)
    
    # Save the .msg file to the .pst file
    $mailItem.Move($pstFile)
    
    # Close the .msg file
    $mailItem.Close(0)
    
    Write-Host "Converted $($msgFile.FullName) to PST."
}

# Recursively search through subfolders
$subFolders = Get-ChildItem -Path $folderPath -Directory
foreach ($subFolder in $subFolders) {
    ConvertToPST -folderPath $subFolder.FullName
}

}

Call the function with the root folder

ConvertToPST -folderPath $rootFolder

Quit Outlook

$outlook.Quit()

Write-Host "Conversion complete."

  • ERROR: You cannot call a method on a null-valued expression. At line:25 char:9
  •     $mailItem = $outlook.Session.OpenSharedItem($msgFile.FullName ...
    
  •     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidOperation: (:) [], RuntimeException
    • FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression. At line:28 char:9

  •     $mailItem.Move($pstFile)
    
  •     ~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidOperation: (:) [], RuntimeException
    • FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression. At line:31 char:9

  •     $mailItem.Close(0)
    
  •     ~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidOperation: (:) [], RuntimeException
    • FullyQualifiedErrorId : InvokeMethodOnNull
0

There are 0 best solutions below