how do you fix corrupted files when you download outlook using powershell?

This is likely because you attempt to save every single attachment to the same file name on disk with the $($_.attachments).saveasfile($outpath) statement.

Change this:

$filename = $($_.attachments | where filename -match '.xlsm').filename
foreach($file in $filename)
{
    $outpath = join-path $filepath $file
    $($_.attachments).saveasfile($outpath)
}

to:

foreach($attachment in $_.attachments)
{
    if($attachment.Filename -like '*.xlsm'){
        $outpath = Join-Path $filepath $attachment.Filename
        # Only save this particular attachment to disk - not all of them
        $attachment.SaveAsFile($outpath)
    }
}

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top