# link to the folder $olFolderPath = "\\alexander.warme@urbanandmainlines.com\Inbox\Checkudvej" # set the desired file name $attachmentFileName = 'checkudvej' # set the location to temporary file $filePath = "C:\Users\a.warme\Downloads\mailfile" # use MAPI name space $outlook = new-object -com outlook.application; $mapi = $outlook.GetNameSpace("MAPI"); # set the Inbox folder id $olDefaultFolderInbox = 6 $inbox = $mapi.GetDefaultFolder($olDefaultFolderInbox) # access the target subfolder $olTargetFolder = $inbox.Folders | Where-Object { $_.FolderPath -eq $olFolderPath } # load emails $emails = $olTargetFolder.Items # process the emails foreach ($email in $emails) { # format the timestamp $timestamp = $email.ReceivedTime.ToString("yyyyMMddhhmmss") # filter out the attachments foreach ($attachment in $email.Attachments) { write-host "ost1" Write-Host $attachment.FileName write-Host "ost2" if ($attachment.FileName -like "*.xlsx"){ write-host "ost4" # insert the timestamp into the file name $fileName = $attachment.FileName #$fileName = $fileName.Insert($fileName.IndexOf('.'),$timestamp) # save the attachment $attachment.saveasfile((Join-Path $filePath $fileName)) } } }