# Powershell email

**URL:** <https://forums.powershell.org/t/powershell-email/10750>\
**Category:** PowerShell Help\
**Created:** [June 4, 2018, 3:22am UTC](https://forums.powershell.org/t/powershell-email/10750 "2018-06-04T03:22:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![krishna17mk](https://avatars.discourse-cdn.com/v4/letter/k/22d042/32.png) [@krishna17mk](https://forums.powershell.org/u/krishna17mk)\
**Post date:** [June 4, 2018, 3:22am UTC](https://forums.powershell.org/t/powershell-email/10750/1 "2018-06-04T03:22:38Z")

</div>

$File=“gci \abcd\efgh\ijkl | select -last 1”  
$From = “[adcb@gmail.com](mailto:adcb@gmail.com)”  
$To = “[efgh@gmail.com](mailto:efgh@gmail.com)”  
$Attachment = $File  
$Subject = “Here’s the Email Subject”  
$Body = “This is what I want to say”  
$SMTPServer = “[smtp.gmail.com](http://smtp.gmail.com)”  
Send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -SmtpServer $SMTPServer -Attachments $Attachment

This is the error I receive:  
Send-MailMessage : Illegal characters in path.  
At line:1 char:1

- Send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -SmtpServer $ …
- 

```auto

```

- CategoryInfo : NotSpecified: (🙂 [Send-MailMessage], ArgumentException
- FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.SendMailMessage

What am I doing wrong here?  
Thanks

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 4, 2018, 4:31am UTC](https://forums.powershell.org/t/powershell-email/10750/2 "2018-06-04T04:31:52Z")

</div>

What’s in your variable $Attachment? How does it look?

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [June 4, 2018, 7:45am UTC](https://forums.powershell.org/t/powershell-email/10750/3 "2018-06-04T07:45:15Z")

</div>

Leverage Get-Help to understand what is expected for the parameter:

```
Get-Help Send-MailMessage -Full
```

```
PARAMETERS
    -Attachments 
        Specifies the path and file names of files to be attached to the email message. You can use this parameter or pipe the paths and file names to Send-MailMessage .
        
        Required? false
        Position? named
        Default value None
        Accept pipeline input? True (ByValue)
        Accept wildcard characters? false
```

The String indicates a string array, specifically an array of paths. If you run your command:

```
PS C:\Users\Rob> Get-ChildItem -Path C:\Scripts | Select -Last 1

    Directory: C:\Scripts

Mode LastWriteTime Length Name                                                                                                                                             
---- ------------- ------ ----                                                                                                                                             
-a---- 5/28/2018 1:41 PM 202 test.csv
```

It is returning an object, so you need to get a Property of that object, which is FullName. Additionally, if you look at [about\_Splatting](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_splatting?view=powershell-6), you can make the code much cleaner:

```
$File = (Get-ChildItem -Path C:\Scripts | Select -ExpandProperty FullName -Last 1)

$mailParams = @{
    From = "adcb@gmail.com"
    To = "efgh@gmail.com"
    Attachment = $File
    Subject = "Here's the Email Subject"
    Body = "This is what I want to say"
    SMTPServer = "smtp.gmail.com"
}

Send-MailMessage @mailParams
```

---

<div class="post-metadata">

**Author:** ![bo1](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/bo1/32/33_2.png) [@bo1](https://forums.powershell.org/u/bo1)\
**Post date:** [June 4, 2018, 7:46am UTC](https://forums.powershell.org/t/powershell-email/10750/4 "2018-06-04T07:46:05Z")

</div>

I believe the issue is with your file variable. You have double quotes around the entire command. Use the -LiteralPath parameter with double quotes around the value if necessary. As it is now, $file is nothing more than a PowerShell command saved as a string. I think the -attatchment parameter probably wants a full path to a file? Not in front of a computer right now so can’t confirm. But your file variable stood out to me as a likely issue.

---

<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:34pm UTC](https://forums.powershell.org/t/powershell-email/10750/5 "2024-05-16T20:34:59Z")

</div>

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


