Delete mail based on subject from outlook by power shell script - powershell

I wrote a script to delete particular mails from particular outlook account but it's not deleting mails based on mail subject . Can anybody tell me what is wrong in my code
CODE
$Outlook = New-Object -ComObject Outlook.Application
# Delete an Email from the folder Inbox with Subject Title "Action"
$EmailInFolderToDelete = $Outlook.Session.Folders.Item(1).Folders.Item("Inbox").Items
$EmailInFolderToDelete | ft SentOn, Subject, SenderName, To, Sensitivity -AutoSize -Wrap
$EmailToDelete = $EmailInFolderToDelete | Where-Object {$_.Subject -eq "Test mail";}
$EmailToDelete.Delete()
It's not showing the desired result and not deleting the particular mail from particular outlook account . Can anybody help me on this .

Based on the code on this hey scripting guy blog post. It worked just fine.
$olFolders = “Microsoft.Office.Interop.Outlook.olDefaultFolders” -as [type]
$outlook = new-object -comobject outlook.application
$namespace = $outlook.GetNameSpace(“MAPI”)
$folder = $namespace.getDefaultFolder($olFolders::olFolderInBox)
$emailToDelete = $folder.items | Where-Object {$_.Subject -eq "Test mail";}
$EmailToDelete.Delete()

Related

Sending email once

For logging tasks I need to send me an email through Outlook. I wrote some code like this:
$outlook = New-Object -ComObject Outlook.Application
$namespace = $outlook.GetNameSpace("MAPI")
$olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
$InboxDef = $namespace.GetDefaultFolder($olFolders::olFolderInBox)
$InboxDef.FullFolderPath -match "^\\\\(.*)\\Inbox$" | Out-Null
$recipient = $matches[1]
$email = $outlook.CreateItem(0)
$email.To = "$recipient"
$email.Subject = "Title"
$email.Body = "Text"
$email.Send()
$Outlook.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Outlook) | Out-Null
When I subsequently launch the Outlook client I see the email sent twice
The code looks good. One thing which is not clear enough is setting a recipient for the outgoing email:
$InboxDef.FullFolderPath -match "^\\\\(.*)\\Inbox$" | Out-Null
$recipient = $matches[1]
It is not clear what value is used in the code. To make sure the property is set correctly I'd suggest using the Recipients property of the MailItem class instead. The Recipients.Add method creates a new recipient in the Recipients collection. Then don't forget to use the Recipient.Resolve
method which attempts to resolve a Recipient object against the Address Book.
Read more about that in the article which I wrote for the technical blog, see How To: Fill TO,CC and BCC fields in Outlook programmatically.

PowerShell outlook shows saved name instead of full email address

I am trying to retrieve sent emails from my outlook using powershell. When i run below code, it pulls the details, but it prints the name of the person email sent to instead of their email address. How can i get user full email address instead of their name?
Function Get-OutlookSentItems {
Add-type -assembly “Microsoft.Office.Interop.Outlook” | out-null
$olFolders = “Microsoft.Office.Interop.Outlook.olDefaultFolders” -as [type]
$outlook = new-object -comobject outlook.application
$namespace = $outlook.GetNameSpace(“MAPI”)
$folder = $namespace.getDefaultFolder($olFolders::olFolderSentMail)
$folder.items |
Select-Object -Property Subject, SentOn, Importance, To
} #end function Get-OutlookSentItems
Get-OutlookSentItems
Current output Test,user
Expected output testuser#test.com

Use found Data from Windows ID and put it into an email through Powershell

I'm trying to create a Powershell script that gets the specific part of a text file, read it and then put it into the contents of an email and sends it. This is what I currently have:
$logs = (Get-EventLog system | where {$_.InstanceId -eq 7001 -and
$_.TimeWritten -gt (Get-Date).Adddays(-1)}).TimeWritten | Out-String
#to file
$logs | Out-File ".\Results.txt"
#create COM object named Outlook
$Outlook = New-Object -ComObject Outlook.Application
#create Outlook MailItem named Mail using CreateItem() method
$Mail = $Outlook.CreateItem(0)
#add properties as desired
$Mail.To = "SomeMailAddress.com"
$Mail.Subject = "Time"
$Mail.Body = $logs
#send message
$Mail.Send()
#quit and cleanup
$Outlook.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Outlook) | Out-Null
I'm able to create the text file, output the data, I believe the Get-Content is getting that specific portion of time but I'm not sure how to use Set-Content and put that into the email. Any suggestions/help would be appreciated
The simplest way to send email through PowerShell is by using Send-MailMessge.
Below is how you would send using the Outlook ComOjbect.
Note: If you have to use the outlook comobject, make sure you run PowerShell and outlook the same way with the same account.
Example:
$logs = (Get-EventLog system | where {$_.InstanceId -eq 7001 -and $_.TimeWritten -gt (Get-Date).Adddays(-1)}).TimeWritten | Out-String
#create COM object named Outlook
$Outlook = New-Object -ComObject Outlook.Application
#create Outlook MailItem named Mail using CreateItem() method
$Mail = $Outlook.CreateItem(0)
#add properties as desired
$Mail.To = "jrider#yourDomain.com"
$Mail.Subject = "Time"
$Mail.Body = $logs
#send message
$Mail.Send()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Outlook) | Out-Null

Is it possible to extract recipient email address from a .msg file? [duplicate]

This question already has an answer here:
How to get email address from the emails inside an Oulook folder via PowerShell?
(1 answer)
Closed 4 years ago.
In order to compile a list of recipients from a batch of .msg files, I'm trying to achieve this with powershell. I can grab the Recipient name, but not their emails. Their address entry shows as System._ComObject
Any advice to what I'm doing wrong and how I can fix this? Thank you.
$outlook = New-Object -ComObject Outlook.Application
$namespace = $outlook.GetNameSpace("MAPI")
Get-ChildItem $msgPath -Filter *.msg |
ForEach-Object{
$msg = $outlook.Session.OpenSharedItem($_.FullName)
$recipient = $msg.Recipients
$address = $recipient.Address
$recipient
}
$outlook.quit()
Thanks to Palle Due: https://stackoverflow.com/a/47264921/361842
$outlook = New-Object -ComObject Outlook.Application
$namespace = $outlook.GetNameSpace("MAPI")
Get-ChildItem $msgPath -Filter *.msg |
ForEach-Object{
$msg = $outlook.Session.OpenSharedItem($_.FullName)
$msg.Recipients | ForEach-Object{
$_.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x39FE001E")
}
}
$outlook.quit()
Regarding the special property value; there's a helpful list here: https://stackoverflow.com/a/45296809/361842

Outlook Powershell Script

I have been doing some digging and trying to find a way to remove a PST from Outlook with a script. I don't entirely know how to script but am trying to learn. I found this old Stackoverflow but I am unsure how to actually enter the information I need into it.
How to disconnect PST file from Outlook using Powershell?
The script in question is below.
$Outlook = new-object -com outlook.application
$Namespace = $Outlook.getNamespace("MAPI")
$PSTtoDelete = "c:\test\pst.pst"
$PST = $namespace.Stores | ? {$_.FilePath -eq $PSTtoDelete}
$PSTRoot = $PST.GetRootFolder()
$PSTFolder = $namespace.Folders.Item($PSTRoot.Name)
$namespace.GetType().InvokeMember('RemoveStore',[System.Reflection.BindingFlags]::InvokeMethod,$null,$namespace,($PSTFolder))
I understand the third line, where to enter the file path to the PST itself but I am not sure what to enter for the rest of the lines.
I know this is a total newbie question, but any help would be super appreciated.
Thank you!
Here is a function i just wrote hope it helps
function Remove-OutlookStore($StoreFilePath){
get-process | where { $_.Name -like "Outlook" }| kill
$Outlook = new-object -com outlook.application
$Namespace = $Outlook.GetNamespace("mapi")
$Store = $namespace.Stores | ?{$_.FilePath -like $StoreFilePath} | %{$_}
$namespace.RemoveStore($Store.GetRootFolder())
get-process | where { $_.Name -like "Outlook" }| kill
}
Remove-OutlookStore -StoreFilePath C:\Test\Test.pst