Outlook - Powershell

I am Stuck at a powershell Script. I am trying to reply a email in outlook using powershell.
i have only 2 methods.
$email.reply()=the $email(is the email i want to reply)
$email.replyall()

How can i add additional email address in this.
if i do $email.recipients.add(“new@email.com”)
i got error
please help

Hi, welcome to the forum :wave:

You’ve not provided much information, but I think the problem you’re having is because you’re trying to add the recipient to the message you want to reply to, rather than to the reply itself.

Try it like this:

$reply = $email.reply()
$reply.Recipients.Add("fred@example.com")

If that doesn’t fix it. Please provide your code and the full error message so that we can test and investigate further.

When posting code and error messages, please ensure you format it properly:

How to format code on PowerShell.org

2 Likes

this is my code.
I did try add recipient too

$x=abc@example.com
$Sendername = Get-Mailbox -Identity $x | Select Displayname -ErrorAction stop
$ol = New-Object -ComObject outlook.application
Add-Type -assembly “Microsoft.Office.Interop.Outlook”
$Outlook = New-Object -comobject Outlook.Application
$namespace = $Outlook.GetNameSpace(“MAPI”)

$mailb = $namespace.Folders | where {$.fullfolderpath -eq “\end_user_services@example.ca”}
$mailbinbox = $mailb.Folders | where {$
.Name -eq “Inbox”}
$mailbinboxemail = $mailbinbox.Items
$mailmess = $mailbinboxemail | where {(($.SenderEmailAddress -like (“123@.regers.com*” -or $x)) -or (($.sendername -eq ($Sendername.DisplayName)))) }
$useremail = $mailmess | where {($.subject -like “case*”) -and (($.body -match ($x -or “abc@123.ca”) -or ($_.body -match ($sendername.DisplayName)))) }

if ($useremail -eq $null) {
Write-Host “#####################################################################################################################” -ForegroundColor Green
Write-host “…No email recived …” -ForegroundColor Red
Write-Host “#####################################################################################################################” -ForegroundColor Green
}
else
{
if (($useremail.to -like ($Sendername.DisplayName)))
{
$reply = $useremail.Recipients.Add(“$($cred.UserName)”)
$reply = $useremail.Recipients.ResolveAll()
$reply = $useremail.Reply()
$reply.body = “Hi $($Sendername.DisplayName),n The device has been removed from quarantine. It may take up to 1 hour to synchronize email on your phone.n
n Kind Regards,n
Name.Lastname”

$reply.send()

}
else
{
$reply = $useremail.ReplyRecipients.Add(“$($x)”)
#$reply = $useremail.Recipients.ResolveAll()
$reply = $useremail.Recipients.ResolveAll()
$reply = $useremail.ReplyAll()
$reply.body = “Hi $($Sendername.DisplayName),n The device has been removed from quarantine. It may take up to 1 hour to synchronize email on your phone.n
n Kind Regards,n”

$reply.send()

}
}

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

You still haven’t provided the error message. The one thing that jumps out at me from your code is these sections:

$reply = $useremail.Recipients.Add(“$($cred.UserName)”)
$reply = $useremail.Recipients.ResolveAll()
$reply = $useremail.Reply()
$reply = $useremail.ReplyRecipients.Add(“$($x)”)
$reply = $useremail.Recipients.ResolveAll()
$reply = $useremail.ReplyAll()

As stated in my previous reply, I think you need to add the recipients to the reply:

$reply = $useremail.Reply()
$reply.Recipients.Add("mail@example.com")

Before responding, please read the links above about how to format your code.

1 Like

To add additional email addresses to an email reply in Outlook using PowerShell, you can use the Add method of the Recipients property of the MailItem object. Here is an example of how you could do this:

Copy code

$email.Recipients.Add("new@email.com")
$email.Recipients.ResolveAll()

The Add method allows you to add a new recipient to the list of recipients for the email. The ResolveAll method is then used to resolve the names of the recipients and ensure that they are valid.

You can also use the Add method to add multiple recipients at once by separating the email addresses with a comma:

Copy code

$email.Recipients.Add("new1@email.com, new2@email.com, new3@email.com")
$email.Recipients.ResolveAll()

After you have added the additional email addresses, you can use the Reply or ReplyAll method to send the email reply as usual.

Copy code

$email.Reply()

I hope this helps! Let me know if you have any other questions.

Carlotta Krajcik
Power Platform Consulting Services

Thank You for reply.

I have tried that , let me post my code:

$x = "example@email.com
$useremail = "the email i narrow down and want to reply to. 

$reply=$useremail.Recipients.Add("$x")
(problem comes when i add this $x here, and when i check the get-member on $reply, it will have only 8 property. it will not give me .body or .send(). 
$reply=$useremail.Reply()
I again get .body property by this. 
$reply=$useremail.Body ="Hi $($Sname.DisplayName),`n
	The device has been removed from quarantine. It may take up to 1 hour to synchronize email on your phone.`n
	`n
	Kind Regards,`n"
Here it again , i cant find send property . so i have to run this reply() again. to activate .send().
$reply=$useremail.Reply()
     
	$reply.send()

It send the email and reply but i cant see the origional email in my inbox.

You should slow down and read what people are suggesting to you. You try to add the emails to the ORIGINAL email, not to the REPLY. Store the reply in a variable and only work on the reply variable. Don’t add to the $useremail variable.

Maybe these notes on this example I typed up for you will help. Stop ignoring the help you are receiving please.

# instatiate outlook and connect to the default profile
$outlook = new-object -comobject outlook.application
$namespace = $outlook.GetNameSpace("MAPI")

# this is the inbox folder
$inbox = $namespace.GetDefaultFolder(6)

# The tostring('g') method will put the date in an outlook filter friendly format
$starttime = (Get-Date).AddMinutes(-2).ToString('g')

# this will find an item newer than the starttime with a specific subject.
# Note: The RE: and FWD: is NOT part of the subject, so all emails in chain have the same
$useremail = $inbox.Items.Find("[receivedtime] > '$starttime' And [subject] = 'test new'")

# this takes the email, creates a reply email object. This is the object you need to manipulate
# Do not use $useremail again, it is not the reply. 
$reply = $useremail.Reply()

# if there is more than one email to add to the TO field, use a loop. Otherwise, simply add the one email.
# Remember, the original sender is already a recipeint on the reply.
$emaillist = 'user@domain.com','user2@domain.com'

foreach($email in $emaillist){
    # capture the output to keep the pipeline clean
    $null = $reply.Recipients.Add($email)
}

# if you need to add CC or BCC, just give a single string with semicolon delimited addresses to it.
$reply.CC = 'user3@domain.com; user4@domain.com'

# if you want to retain the CC addresses and add to it, then add to it. (But you would've needed to do ReplyAll() instead of Reply() earlier)
$reply.CC = $reply.CC += '; user3@domain.com; user4@domain.com'

# if you really want the exact same body in the original, you can set the reply body to the same, but this seems weird. 
# Generally you would insert your own body of text into the body. However this would be one of the few times using the original email variable is fine. 
$reply.Body = $useremail.body

# send the reply. 
$reply.Send()
2 Likes

Thank you ! it works. Thank you so much !