MaxM
MaxM

Reputation: 23

Powershell: Get Attachments of E-Mails from PST File

Currently, I'm trying to get attachment names from E-Mails within a PST-File. I want to do this via Powershell. My code bracket works fine so far, except for one thing. It Just writes System.__ComObject as the attachments name. Any ideas?

## Path where the PSTFiles are
$PSTArr = Get-ChildItem -Path .\ -Recurse
$Attachments = @()

## Processing

ForEach ($PST in $PSTArr) {
    if ($PST.Name -like "*.pst") {
    [string]$pstPath = $PST.FullName
    Write-Output ("Checking File: " + $pstPath)

    # Lets see if there is a running outlook process
    $oProc = ( Get-Process | where { $_.Name -eq "OUTLOOK" } )
    if ( $oProc -eq $null ) { Start-Process outlook -WindowStyle Hidden; Start-Sleep -Seconds 5 }
    $outlook = New-Object -ComObject Outlook.Application

    # Assign namespace
    $namespace = $outlook.GetNamespace("MAPI")

    # Add PST
    $namespace.AddStore($pstPath)
    $pstStore = ( $nameSpace.Stores | where { $_.FilePath -eq $pstPath } )

    # Add RootFolder of the PST
    $pstRootFolder = $pstStore.GetRootFolder()

    # Get attachments of the E-Mails
    $Attachments += $pstRootFolder.Items | Select Attachment

    # Disconnect PST File
    $namespace.GetType().InvokeMember('RemoveStore',[System.Reflection.BindingFlags]::InvokeMethod,$null,$namespace,($pstRootFolder))

    }
}

(I'm looping through a Folder with PST's, which works fine)

Thanks for help / answers

Upvotes: 2

Views: 3447

Answers (1)

sodawillow
sodawillow

Reputation: 13176

Try this:

$Attachments = $pstRootFolder.Items | ForEach-Object {
    $_.Attachments | Select-Object -ExpandProperty FileName
}

Upvotes: 1

Related Questions