24

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.
physlexic
  • 826
  • 2
  • 9
  • 21
  • 1
    Possible duplicate of [How to concatenate strings and variables in PowerShell?](http://stackoverflow.com/questions/15113413/how-to-concatenate-strings-and-variables-in-powershell) – BioGenX Mar 17 '17 at 15:52

3 Answers3

39

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.

henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
10
echo ("The file "+ $filename +" has been processed.")
4b0
  • 21,981
  • 30
  • 95
  • 142
tom
  • 101
  • 1
  • 2
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.
BioGenX
  • 402
  • 3
  • 11
  • 1
    When writing out an `object` then `Write-Host` will give you the object's type, not its contents. Better to use`Write-Output` – Dr Rob Lang Jul 10 '18 at 15:18