Powershell: How to pass empty body to Send-MailMessage?


I have this simple script:

It is written that way so that one could run it without any arguments, in which case a test message would be sent to the author (me).

Currently the script fails, because passing $null in the -Body argument is not allowed:

I have three possible solutions:

  • Two Send-MailMessage command statements:

  • Using Invoke-Expression:

  • Simulating an empty body with a single space character:

All of these solutions look bad to me, because they all have this smell of being just hacks. I must be missing something really basic here, so my question is how can I pass the empty body without having to resort to these hacks?



You can use a “splat” variable to pass parameters indirectly:

Note use of @ to pass hash table of parameter-name parameter-value pairs (you can of course pass all the parameters this way, eg. in the initialisation of $extraParams).


Powershell: How to pass empty body to Send-MailMessage? by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply