28

I'm running a script, and I want it to print a "statement + variable + statement" at the end [when successful]. I've tried a few thing but it always returns as 3 separate lines, instead of one. The Echo "" before and after is just to make it easier to read when printed by spacing it out, I've tried it with and without and I get the same result.

$filename = "foo.csv"

    echo ""
    echo "The file" $filename "has been processed."
    echo ""

I get this:

The file
foo.csv
has been processed.
1

3 Answers 3

42

If you use double quotes you can reference the variable directly in the string as they allow variable expansion, while single quotes do not allow this.

$filename = "foo.csv"
Write-Output "The file $filename has been processed."

-> The file foo.csv has been processed.

Also, echo is actually just an alias for Write-Output, so I've used the full name.

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

4 Comments

So simple. I got thrown off because it gets grayed out. I thought it was being converted to plain text. Thanks.
I'd certainly recommend reading up on double vs single quotes, such a small thing can have a very big effect :)
this not work properly with $args[0] if they are few - its print it all args :/
@GorodeckijDimitrij yes, arrays and strings need to be handled differently.
14
echo ("The file "+ $filename +" has been processed.")

1 Comment

Please add a description to ur answer
7

In powershell, you can use Write-Host as follows:

$filename = "foo.csv"
Write-Host 'The file' $filename 'has been processed.'

-> The file foo.csv has been processed.

1 Comment

When writing out an object then Write-Host will give you the object's type, not its contents. Better to useWrite-Output

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.