# Enclose variable value in double quotes

**URL:** <https://forums.powershell.org/t/enclose-variable-value-in-double-quotes/17402>\
**Category:** PowerShell Help\
**Created:** [August 25, 2021, 12:17am UTC](https://forums.powershell.org/t/enclose-variable-value-in-double-quotes/17402 "2021-08-25T00:17:09Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rahul20](https://avatars.discourse-cdn.com/v4/letter/r/838e76/32.png) [@Rahul20](https://forums.powershell.org/u/Rahul20)\
**Post date:** [August 25, 2021, 12:17am UTC](https://forums.powershell.org/t/enclose-variable-value-in-double-quotes/17402/1 "2021-08-25T00:17:10Z")

</div>

Hello Everyone,

I have a variable containing something like below  
**[abdc@gmail.com](mailto:abdc@gmail.com)";"qwer@gmail.com**

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

---

<div class="post-metadata">

**Author:** ![ferc](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ferc/32/2135_2.png) [@ferc](https://forums.powershell.org/u/ferc)\
**Post date:** [August 25, 2021, 12:33am UTC](https://forums.powershell.org/t/enclose-variable-value-in-double-quotes/17402/2 "2021-08-25T00:33:23Z")

</div>

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

```auto
'"abdc@gmail.com";"qwer@gmail.com"'

```

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

```auto
'abdc@gmail.com'

```

Or

```auto
"""abdc@gmail.com"""

```

---

<div class="post-metadata">

**Author:** ![Rahul20](https://avatars.discourse-cdn.com/v4/letter/r/838e76/32.png) [@Rahul20](https://forums.powershell.org/u/Rahul20)\
**Post date:** [August 25, 2021, 12:41am UTC](https://forums.powershell.org/t/enclose-variable-value-in-double-quotes/17402/3 "2021-08-25T00:41:37Z")

</div>

@ferc, Thanks for answering.

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

```auto
$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

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

```

Even I tried

```auto
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

---

<div class="post-metadata">

**Author:** ![ferc](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ferc/32/2135_2.png) [@ferc](https://forums.powershell.org/u/ferc)\
**Post date:** [August 25, 2021, 1:15am UTC](https://forums.powershell.org/t/enclose-variable-value-in-double-quotes/17402/4 "2021-08-25T01:15:51Z")

</div>

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:

```auto
$Maillist.Count

```

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.

```auto
$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.

---

<div class="post-metadata">

**Author:** ![Chen.Chen](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/chen.chen/32/1689_2.png) [@Chen.Chen](https://forums.powershell.org/u/Chen.Chen)\
**Post date:** [August 25, 2021, 1:23am UTC](https://forums.powershell.org/t/enclose-variable-value-in-double-quotes/17402/5 "2021-08-25T01:23:31Z")

</div>

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](mailto:user01@fabrikam.com)’ -To ‘User02 [user02@fabrikam.com](mailto:user02@fabrikam.com)’, ‘User03 [user03@fabrikam.com](mailto: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](http://smtp.fabrikam.com)’

“[abc@gmail.com](mailto: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)

 ![image](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/1/1e84652ca21689f4ef5a1e0d2d1fd053b4c5fc2e.png)

---

<div class="post-metadata">

**Author:** ![Rahul20](https://avatars.discourse-cdn.com/v4/letter/r/838e76/32.png) [@Rahul20](https://forums.powershell.org/u/Rahul20)\
**Post date:** [August 25, 2021, 1:24am UTC](https://forums.powershell.org/t/enclose-variable-value-in-double-quotes/17402/6 "2021-08-25T01:24:59Z")

</div>

Thanks @ferc .

It worked 🙂

---

<div class="post-metadata">

**Author:** ![Rahul20](https://avatars.discourse-cdn.com/v4/letter/r/838e76/32.png) [@Rahul20](https://forums.powershell.org/u/Rahul20)\
**Post date:** [August 25, 2021, 1:27am UTC](https://forums.powershell.org/t/enclose-variable-value-in-double-quotes/17402/7 "2021-08-25T01:27:20Z")

</div>

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

> Mail  
> [abcd@gmail.com](mailto:abcd@gmail.com)  
> [qwer@gmail.com](mailto:qwer@gmail.com).

But still it’s working with txt files

---

<div class="post-metadata">

**Author:** ![Chen.Chen](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/chen.chen/32/1689_2.png) [@Chen.Chen](https://forums.powershell.org/u/Chen.Chen)\
**Post date:** [August 25, 2021, 1:31am UTC](https://forums.powershell.org/t/enclose-variable-value-in-double-quotes/17402/8 "2021-08-25T01:31:09Z")

</div>

Could you please upload the error message ?

---

<div class="post-metadata">

**Author:** ![Rahul20](https://avatars.discourse-cdn.com/v4/letter/r/838e76/32.png) [@Rahul20](https://forums.powershell.org/u/Rahul20)\
**Post date:** [August 25, 2021, 1:49am UTC](https://forums.powershell.org/t/enclose-variable-value-in-double-quotes/17402/9 "2021-08-25T01:49:15Z")

</div>

I was getting the below error:

```auto
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 
   rationException
    + 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

```

---

<div class="post-metadata">

**Author:** ![Rahul20](https://avatars.discourse-cdn.com/v4/letter/r/838e76/32.png) [@Rahul20](https://forums.powershell.org/u/Rahul20)\
**Post date:** [August 25, 2021, 1:51am UTC](https://forums.powershell.org/t/enclose-variable-value-in-double-quotes/17402/10 "2021-08-25T01:51:19Z")

</div>

@ferc

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.

---

<div class="post-metadata">

**Author:** ![Rahul20](https://avatars.discourse-cdn.com/v4/letter/r/838e76/32.png) [@Rahul20](https://forums.powershell.org/u/Rahul20)\
**Post date:** [August 25, 2021, 1:54am UTC](https://forums.powershell.org/t/enclose-variable-value-in-double-quotes/17402/11 "2021-08-25T01:54:04Z")

</div>

@ferc, Please ignore above.

I found a way using below:

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

```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:21pm UTC](https://forums.powershell.org/t/enclose-variable-value-in-double-quotes/17402/12 "2024-05-16T20:21:44Z")

</div>

Closed on May 16, 2024, 8:21pm UTC


