Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Script to pull outlook attachments from outlook folder then print by date or save name

$
0
0

I have a user that gets many attachemnts in their email. We would like to pull the attachments out save them by name or date then print them in that order to keep them grouped together when an email has multiple attachments. I have a script below that only works in the ISE:

[void] [Reflection.Assembly]::LoadWithPartialName("Framework64")
[void] [Reflection.Assembly]::LoadWithPartialName("Microsoft.PowerShell.ISECommon")
[void] [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [Reflection.Assembly]::LoadWithPartialName("System.Management.Automation")
[void] [Reflection.Assembly]::LoadWithPartialName("Microsoft.PowerShell.GPowerShell")
[void] [Reflection.Assembly]::LoadWithPartialName("System.ComponentModel.Composition")
[void] [Reflection.Assembly]::LoadWithPartialName("Microsoft.PowerShell.Editor")
[void] [Reflection.Assembly]::LoadWithPartialName("PresentationFramework")
[void] [Reflection.Assembly]::LoadWithPartialName("WindowsBase")
[void] [Reflection.Assembly]::LoadWithPartialName("PresentationCore")
[void] [Reflection.Assembly]::LoadWithPartialName("System.Xaml")
[void] [Reflection.Assembly]::LoadWithPartialName("System.Configuration")
[void] [Reflection.Assembly]::LoadWithPartialName("UIAutomationProvider")
[void] [Reflection.Assembly]::LoadWithPartialName("System.Management")
[void] [Reflection.Assembly]::LoadWithPartialName("System.DirectoryServices")
[void] [Reflection.Assembly]::LoadWithPartialName("System.Numerics")
[void] [Reflection.Assembly]::LoadWithPartialName("PresentationFramework.Aero")
[void] [Reflection.Assembly]::LoadWithPartialName("UIAutomationTypes")
[void] [Reflection.Assembly]::LoadWithPartialName("System.Transactions")
[void] [Reflection.Assembly]::LoadWithPartialName("Microsoft.PowerShell.GraphicalHost")
[void] [Reflection.Assembly]::LoadWithPartialName("Microsoft.CSharp")
[void] [Reflection.Assembly]::LoadWithPartialName("System.Data")
[void] [Reflection.Assembly]::LoadWithPartialName("System.Runtime.DurableInstancing")
[void] [Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Interop.Outlook")
[void] [Reflection.Assembly]::LoadWithPartialName("office")
[void] [Reflection.Assembly]::LoadWithPartialName("CustomMarshalers")
[void] [Reflection.Assembly]::LoadWithPartialName("Microsoft.WSMan.Management")
[void] [Reflection.Assembly]::LoadWithPartialName("Microsoft.WSMan.Runtime")

#you'll get a popup in outlook at this point where you pick the folder you want to scan

$o = New-Object -comobject outlook.application
$n = $o.GetNamespace(“MAPI”)
$f = $n.pickfolder()
$a = $f.Items
$b = $a.Count

$objShell = new-object -com shell.application
$d=$objShell.BrowseForFolder(0,"Title",0,"\\thoth\shared\reception")
$c= $d.self
$path=$c.path

#now loop through them and grab the attachments
PROCESS {
foreach ($item in $a) 
{ foreach ($attach in $item.Attachments)
    {  if ($item.ReceivedTime -gt (get-date).AddDays(-30)) 
{if ($attach.filename.contains("pdf")){ $attach.saveasfile((Join-Path $path $attach.filename))
}  
   elseif  ($attach.filename.contains("docx"))
              { $attach.saveasfile((Join-Path $path $attach.filename))
            }   
        }
   }
}
BEGIN {}

  PROCESS {}

  END {}

}
}


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>