Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Convert eml files to Folders, word documents and attachments!

$
0
0

A customer of mine had a few years worth of eml files that they needed access to quickly without using outlook! Powershell to the rescue! This script creates a folder with the name of the email, extracts the body of the email and places it in a word doc then saves any attachments in the new folder with the word doc!

#Disable Outlook Signatures# Disable signature$signaturesPath = Join-Path$env:APPDATA"Microsoft\Signatures"Get-ChildItem$signaturesPath | % {Rename-Item$_.FullName ("_" + $_.Name)
}#Set SaveAs Type$olDoc = 4# Load applications$outlook = New-Object -ComObject Outlook.Application#Get FileNames and Message Name$FileNames = Get-ChildItem$PWD *.msg -recurse | Where-object {!$_.psIsContainer -eq$true} | ForEach-Object -Process {$_.FullName}$MsgNames = Get-ChildItem$PWD *.msg -recurse | Where-object {!$_.psIsContainer -eq$true} | ForEach-Object -Process {$_.Name}#Crawl through Filesfor ($i=0;$i-lt$FileNames.Count;$i++) {#Extract Email Text#Set Current Filename$FileName = $FileNames[$i]$MsgName = $MsgNames[$i]#Set Folder Names$FolderName = $FileName.Trim(".msg")#Create New Foldersif ( -not (Test-Path"$FolderName")){New-Item -Path "$FolderName" -ItemType Directory}# Extract message body and save to doc file$MessageBody = $outlook.CreateItemFromTemplate($FileName)#Set new FileName$MsgNameDoc = $MsgName-replace'\.msg$', '.doc'#Set variable to save file to new folder$OutFileName = $FolderName + "\" + $MsgNameDoc$MessageBody.SaveAs($OutFileName, $olDoc)#Extract Email Attachments$MessageBody.Attachments | % {#Work out attachment file name$AttachmentFilename = $MsgName-replace'\.msg$', " - Attachment - $($_.FileName)"#Set variable to save attachments to new folder$AttachmentOutFileName = $FolderName + "\" + $AttachmentFilenameWrite-Host$AttachmentOutFileName# Save attachment$_.SaveAsFile($AttachmentOutFileName)
    }
}# Enable signaturesGet-ChildItem$signaturesPath | % {Rename-Item$_.FullName $_.Name.Substring(1)
}

Viewing all articles
Browse latest Browse all 6937

Trending Articles