Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell Echo Statement + Variable In One line

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.
like image 261
physlexic Avatar asked Mar 17 '17 15:03

physlexic


People also ask

How do I echo a variable value in PowerShell?

The echo command is used to print the variables or strings on the console. The echo command has an alias named “Write-Output” in Windows PowerShell Scripting language. In PowerShell, you can use “echo” and “Write-Output,” which will provide the same output.

What does $_ in PowerShell mean?

The “$_” is said to be the pipeline variable in PowerShell. The “$_” variable is an alias to PowerShell's automatic variable named “$PSItem“. It has multiple use cases such as filtering an item or referring to any specific object.

How do I concatenate strings in PowerShell?

In PowerShell, string concatenation is primarily achieved by using the “+” operator. There are also other ways like enclosing the strings inside double quotes, using a join operator, or using the -f operator. $str1="My name is vignesh."

How do I display an environment variable in PowerShell?

Environment variables in PowerShell are stored as PS drive (Env: ). To retrieve all the environment variables stored in the OS you can use the below command. You can also use dir env: command to retrieve all environment variables and values.


1 Answers

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.

like image 92
henrycarteruk Avatar answered Sep 19 '22 19:09

henrycarteruk