Enclose variable value in double quotes

Hello Everyone,

I have a variable containing something like below

I would like to add " before and after the variable value. How can I do this ? Please help

Assuming the whole thing is a string, including the semicolon and the double quotes, you can use single quotes.


If you want each email address in a different variable you can do the same.




@ferc, Thanks for answering.

Actually I was trying to send mail to multiple users at a time using send-mailmessage

$MailInput= ipcsv -Path .\Desktop\EMAIL.txt
$Maillist = $MailInput.mail -join "`";`""
$EnclosedMailList = "`"$($Maillist)`""

Now I am receiving the desired output, but I am getting error when I use the below command

Send-MailMessage -To $EnclosedMailList -From "Reporting@abcd.com"  -SmtpServer "smtp.abcd.com"-Subject "test Report" 

Even I tried

Send-MailMessage -To @($EnclosedMailList) -From "Reporting@abcd.com"  -SmtpServer "smtp.abcd.com"-Subject "test Report" 

The above didn’t worked too.

Could you please help me

From the help of Send-MailMessage: “-To <System.String>”

This cmdlet accepts multiple strings as input for the To parameter. If you use the -join operator you are basically building a single string. You can verify this by running:


It should return 1

Besides, you should separate the email addresses with comma, not with semicolon.

This should work: remove everything starting with the -join operator and use the resulting variable.

$MailInput= ipcsv -Path .\Desktop\EMAIL.txt
$Maillist = $MailInput.mail

Send-MailMessage -To $Maillist -From "Reporting@abcd.com"  -SmtpServer "smtp.abcd.com" -Subject "test Report"

Let me know how it goes.

1 Like

Hello Rahul,

You can run the get-help send-mailmessage, and you can see the parameter “- To”.
-To <System.String>
The To parameter is required. This parameter specifies the recipient’s email address. If there are multiple recipients, separate their addresses with a comma (,). Enter names (optional) and the email address, such as Name <someone@fabrikam.com>.

and the example 2,

---------------- Example 2: Send an attachment ----------------
Send-MailMessage -From ‘User01 user01@fabrikam.com’ -To ‘User02 user02@fabrikam.com’, ‘User03 user03@fabrikam.com’ -Subject ‘Sending the Attachment’ -Body “Forgot to send the attachment. Sending now.” -Attachments .\data.csv -Priority High -DeliveryNotificationOption OnSuccess, OnFailure -SmtpServer ‘smtp.fabrikam.com

abc@gmail.com;"efg@gmail.com" is not correct.

I notice that your colleciton EMAIL.txt is CSV file. because you use the ipcsv.
So please try save your .txt file as CSV (Comma delimited)(*.csv)

1 Like

Thanks @ferc .

It worked :slight_smile:

@Chen.Chen I have saved the text file as below:


But still it’s working with txt files

Could you please upload the error message ?

I was getting the below error:

Send-MailMessage : A recipient must be specified.
At line:1 char:1
+ Send-MailMessage -To ($EnclosedMailList) -From "Reporting@abcd ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], InvalidOpe 
    + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.SendMailMessage
Send-MailMessage : An invalid character was found in the mail header: '"'.
At line:1 char:1
+ Send-MailMessage -To ($EnclosedMailList) -From "Reporting@abcd ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidType: (:) [Send-MailMessage], FormatException
    + FullyQualifiedErrorId : FormatException,Microsoft.PowerShell.Commands.SendMailMessage


If I have to directly use the user mail addresses without adding Mail in text file, how can I achieve this directly from text file ?

In this case, I have added Mail in the beginning of text file. I would like to automate this task, can you help.

@ferc, Please ignore above.

I found a way using below:

$MailInput= ipcsv -Path .\Desktop\EMAIL.txt -Header Mail