1

I'm fairly new to PowerShell. I've created a variable which contains some data which I then pass on to an email generator:

$strEmailFile = "C:\Testing\SomeTextFile.txt"

cat $strEmailFile | E:\sendmail.exe -t

My problem is I don't want to hard-code the drive for the email generating software. So I would like the code to be something like this:

$strEmailFile = "C:\Testing\SomeTextFile.txt"

$Dir = "E:"

cat $strEmailFile | $Dir + "\sendmail.exe" -t

But I keep getting the following error:

"Expressions are only allowed as the first element of a pipeline."

Is there a way for me to use the pipeline but not have to hard-code?

1
  • You don't need sendmail.exe with PowerShell when you have Send-MailMessage. Commented Mar 8, 2017 at 18:51

1 Answer 1

1

You should look into Send-MailMessage but the difference between your attempts is that the first one is an expression, and the second one is a string, and you can't pipe into a string.

But if you use the call operator & you could pipe into that:

cat $strEmailFile | & ($Dir + "\sendmail.exe") -t

Note that you need parentheses to first run the expression that concatenates the variable and the string).

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you that helped. Sorry for the delayed response
@S.Hasan 3 years later, you are very welcome! Thanks for the note :)

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.