Is there a way to refresh an Outlook Inbox subfolder in powershell? - powershell

I have a powershell script to get e-mails from an Outlook Inbox subfolder, but it only returns e-mails after three or more executions.
Is there a way to prompt Outlook to refresh the Inbox subfolder before the script prompts the scan?
I am accessing the mailbox through another machine to guarantee the emails are on the subfolder and the flags are cleared.
Here it is a sample of the script:
# Add Interop Assembly
Add-type -AssemblyName "Microsoft.Office.Interop.Outlook" | Out-Null
# Create outlook COM object to search folders
$Outlook = New-Object -ComObject Outlook.Application
$OutlookNS = $Outlook.GetNamespace("MAPI")
$OutlookNS.Logon($null, $null, $false, $false);
$OutlookNS.SendAndReceive($false) | Wait-Job
Write-Output "Getting an email from Outlook..."
$Email = $OutlookNS.Folders.Item($Account).Folders.Item($Folder).Items | ? { ($_.FlagStatus -match 0) } | Sort ReceivedTime | Select -First 1
if($Email){
Write-Output $Email.Subject
} else {
Write-Output "No data"
}
...

Related

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

Delete mail based on subject from outlook by power shell script

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()

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

How to Send email from Draft using Powershell?

I'm trying to automatically send all email saved, which are available in "Draft" using powershell.I've seen VBA can do, but powershell is the future. Sharing code will help other peer to tweak.
#Send all items in the "Drafts" folder that have a "To" address filled in.
#Setup Outlook
Add-Type -assembly "Microsoft.Office.Interop.Outlook"
$Outlook = New-Object -comobject Outlook.Application
$myOutlook = $Outlook.Application
$myNameSpace = $myOutlook.GetNamespace("MAPI")
$myFolders = $myNameSpace.Folders
#Set Draft Folder.
$myDraftsFolder = $myFolders("rsprebitz#idafoundation.org").Folders("Drafts")
#Loop through all Draft Items
$myDraftsFolder.Items|foreach-object {
#Check for "To" address and only send if "To" is filled in.
If ($_.To.trim().length -gt 0) {
#Send Item
$_.Send
}
}
try this
VWP.CS's answer was close but had to make a slight change to get it to work for me:
# Initialize components
Add-Type -assembly "Microsoft.Office.Interop.Outlook"
$Outlook = New-Object -comobject Outlook.Application
$namespace = $Outlook.GetNameSpace("MAPI")
# Set drafts folder
$myDraftsFolder = $namespace.Folders.Item('yourmailboxname').Folders.Item('Drafts')
# Loop through all Draft Items
$myDraftsFolder.Items | foreach-object {
# Check for "To" address and only send if "To" is filled in.
If ($_.To.trim().length -gt 0) {
#Send Item
Write-Verbose "Sending..." -Verbose
$_.Send()
}
}

List Senders from a Shared Mailbox Folder (Recursively)

I am trying to get a full list of senders to an specific shared mailbox i have access to (vía Outlook).
So far I am using this small but handy script to choose the selected folder as I have no idea on how to call as a parameter (Param) the folder I want to check:
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")
$Inbox = $namespace.pickfolder()
$Inbox.items | Select-Object -Property SenderName
After that I filter the output depending on the info I need (in this case, senders).
This forces me to choose the folder MANUALLy, I just want to set it as a parameter to the script.
How should I pass the specific folder as a parameter?.
Thanks in advance and best regards.
If I understood that correctly, you yould create a function where you pass the Inbox Name
function GetSenders
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$True)]
[string]$Inbox
)
Begin
{
Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null
$olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
$outlook = new-object -comobject outlook.application
}
Process
{
"Your Script here - What should happen with the Inbox?"
}
End
{
Write-Host "Script has Finished"
}
}
When you load this Function, you can just write GetSenders -Inbox "InboxName"