3

I have the following PowerShell script

$var = 'abcd'
echo $($var):123:zzz > test.txt

After execution, the content of test.txt becomes

abcd
:123:zzz

How can I avoid newline after abcd?

I mean I want the content of test.txt to be

abcd:123:zzz

How can I do this?

3
  • '$($var):123:zzz' > test.txt or "$var`:wetwet:ew" > test.txt Commented Jan 25, 2017 at 8:50
  • 1
    I found that "$($var):123:zzz" > test.txt works for me. Commented Jan 25, 2017 at 8:54
  • @Seth: The original question title was a bit ambiguous; this question is not about omitting a trailing newline from the output. To your point: Write-Host output cannot be redirected on PSv4-. In PSv5+, Out-File and Set-Content / Add-Content support -NoNewline as well. Commented Jan 25, 2017 at 17:49

3 Answers 3

4

echo is an alias for the Write-Output cmdlet which is not necessary here at all. Just put the desired ouput into the pipeline and pipe it to the Set-Content cmdlet:

"$($var):123:zzz" | Set-Content -Path 'test.txt' -Encoding Unicode

Now, if you want to append another string to the file, use the Add-Content cmdlet:

"anotherLine" | Add-Content test.txt -Encoding Unicode
Sign up to request clarification or add additional context in comments.

5 Comments

My test.txt becomes empty after I execute "$var:123:zzz" | Set-Content -Path 'test.txt'
Does the version of powershell matter? My powershell's version is 2.0
No, this will also work for PowerShell 2.0. Try my edited answer again.
@mklement0 I didn't thought about the encoding at all. Changed it now - thank you. Please feel free to edit my answers if you find such things, I trust you.
@MartinBrandl Thanks for updating; I appreciate the offer to edit your answers myself.
1

Martin Brandl's helpful answer bypasses your problem with a solution that is preferable anyway (no need for Write-Output a.k.a echo).
This answer explains the problem with your approach.

tl;dr

  • Use double quotes:

    • echo "$($var):123:zzz" > test.txt
  • Simplified (no need for echo):

    • "$($var):123:zzz" > test.txt
  • Simplest (note the {}, which are needed to disambiguate the variable name):

    • "${var}:123:zzz" > test.txt
    • "$var`:123:zzz" > test.txt would work too (escaping the :)

Your specific problem is that $($var):123:zzz is parsed as 2 arguments, due to not being enclosed in ".

In other words: The following 2 statements are equivalent:

echo $($var):123:zzz

echo $($var) :123:zzz    

Both yield the following (wether in the console or when redirected to a file), because Write-Output (that echo is an alias for) writes its (stringified) arguments separated by a line break:

abcd
:123:zzz

Since you're passing $($var):123:zzz as an argument to the echo (Write-Output) command, it is parsed in argument mode.

In argument mode, an unquoted subexpression-operator expression such as $($var) at the start of a token is always treated as a separate argument, even if followed directly by more characters.

This answer of mine has more information about how PowerShell parses unquoted tokens in argument mode.

By contrast, double-quoting (enclosing the token in "...") ensures parsing as a single argument (though note that the subexpression will then be stringified first).

Thus, this command would work:

echo "$($var):123:zzz" > test.txt

Or, more simply, given that PowerShell outputs an expression's result by default (Write-Output is usually not needed):

"$($var):123:zzz" > test.txt

Note that while double-quoted strings are used in both commands, the latter command is technically parsed in expression mode (which is PowerShell's other, programming language-like parsing mode), because the command line starts with a ".

Comments

0

If you are trying to add ":123:zzz" to the end of $var you can do this as well;

$var = 'abcd'
echo ($var+':123:zzz') > test.txt

or if you want to add a new line (append) add a second ">";

$var = 'abcd'
echo ($var+':123:zzz') >> test.txt

Comments

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.