Simple script to see any new emails in Office365

Member_2_5306354
Member_2_5306354 used Ask the Experts™
on
Is there a Powershell or VBS or batch script that can be run which will show you quickly similar to an inbox, for Office365 emails, perhaps specifycing
simply any new emails received in an Inbox with From, Subject and Time.. like a quick view?

I wanted to be able to run a script from a menu, perhaps called 1.Email, and then quickly log into Office365 email to see if any new emails have come in etc.. without having to open Outlook or any other major email client..
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
AlexSenior Infrastructure Analyst

Commented:
I have one question, why? I mean you even get a popup with outlook.

Don't bother trying to reinvent the wheel, it'll be longer for you to run the script than it would be if you just opened up outlook and left it open.
Shaun VermaakTechnical Specialist
Awarded 2017
Distinguished Expert 2018

Commented:
Yes, you can. You use EWS (Exchange Web Services)

After connecting
# Get the Mailbox ID of the Inbox folder.
$inboxFolderID = [Microsoft.Exchange.WebServices.Data.FolderId]::new([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailAddress)

# Bind to the inbox folder.
$boundFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($objExchange,$inboxFolderID)
$boundFolder.UnreadCount

Open in new window

https://stackoverflow.com/questions/44160461/checking-number-of-unread-exchange-mails-with-ews

Author

Commented:
Thanks Shaun.  And what would be the script to connect then? Or what is before this??
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Shaun VermaakTechnical Specialist
Awarded 2017
Distinguished Expert 2018

Commented:
It is the first part on that link, the section in OP's question

Author

Commented:
Thanks again Shaun.  Ok, I added credentials etc., however now I don't know where to replace the script you provided or what to remove in order to make it work?

Author

Commented:
This does not work, I found another solution online.
$mail="email@email.com"
$password="enterpasswordhere"

# Set the path to your copy of EWS Managed API
$dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
# Load the Assemply
[void][Reflection.Assembly]::LoadFile($dllpath)

# Create a new Exchange service object
$service = new-object Microsoft.Exchange.WebServices.Data.ExchangeService

#These are your O365 credentials
$Service.Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials($mail,$password)

# this TestUrlCallback is purely a security check
$TestUrlCallback = {
    param ([string] $url)
    if ($url -eq "https://autodiscover-s.outlook.com/autodiscover/autodiscover.xml") {$true} else {$false}
}
# Autodiscover using the mail address set above
$service.AutodiscoverUrl($mail,$TestUrlCallback)

# create Property Set to include body and header of email
$PropertySet = New-Object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)

# set email body to text
$PropertySet.RequestedBodyType = [Microsoft.Exchange.WebServices.Data.BodyType]::Text;

# Set how many emails we want to read at a time
$numOfEmailsToRead = 10

# Index to keep track of where we are up to. Set to 0 initially.
$index = 0
# Do/while loop for paging through the folder
do
{
    # Set what we want to retrieve from the folder. This will grab the first $pagesize emails
    $view = New-Object Microsoft.Exchange.WebServices.Data.ItemView($numOfEmailsToRead,$index)
    # Retrieve the data from the folder
    $findResults = $service.FindItems([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$view)
    foreach ($item in $findResults.Items)
    {
        # load the additional properties for the item
        $item.Load($propertySet)

        # Output the results
        "******************************************"
        ""
        "From: $($item.From.Name)"
        ""
        "Subject: $($item.Subject)"
        ""
        #"Body: $($Item.Body.Text)"
        "******************************************"
        ""
        ""
     
    }
    # Increment $index to next block of emails
    $index += $numOfEmailsToRead
} while ($findResults.MoreAvailable) # Do/While there are more emails to retrieve

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial