0

I am using below PowerShell to pass subject parameter to Send-MailMessage but its not working. Even after passing subject value its still asks for an input for Subject

Get-Date | Select-Object @{Name='Subject';Expression={$_.DateTime}} | Send-MailMessage -From '[email protected]' -To '[email protected]' -SmtpServer 'mysmptserver.COM'

Am I missing something?

0

1 Answer 1

1

The Subject parameter of Send-MailMessage does not accept pipeline input:

PS ~> Get-Help Send-MailMessage -Parameter Subject

-Subject <string>

    Required?                    true
    Position?                    1
    Accept pipeline input?       false
    Parameter set name           (All)
    Aliases                      sub
    Dynamic?                     false

Pipe your data to ForEach-Object and call Send-MailMessage on each iteration instead:

Get-Date | Select-Object @{Name='Subject';Expression={$_.DateTime}} | ForEach-Object {
  Send-MailMessage -Subject $_ -From '[email protected]' -To '[email protected]' -SmtpServer 'mysmptserver.COM' 
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @mathias for quick response. Still its not working and asking for value for subject. Also looked at online documentation and it says Accept pipeline input to True. learn.microsoft.com/en-us/powershell/module/…
@RaviKhambhati My mistake, I forgot to add -Subject $_, answer has been updated now, try again :)
Thanks @Mathias for the update. Does this mean the documentation on Microsoft is incorrect?
@RaviKhambhati You're looking at the documentation for PowerShell 7.2, check out the docs for Windows PowerShell 5.1 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.