66

How can I write a variable to the console without a space after it? There are problems when I try:

$MyVariable = "Some text"
Write-Host "$MyVariableNOSPACES"

I'd like the following output:

Some textNOSPACES
1
  • 1
    Yeah, for all the un/boxing shortcuts Powershell cough "enables", you'd think that, unless you have a variable named $MyVariableN (or another collision) that PS would figure out what you meant. No dice. Commented Dec 5, 2014 at 19:22

7 Answers 7

91

One possibly canonical way is to use curly braces to delineate the name:

$MyVariable = "Some text"
Write-Host "${MyVariable}NOSPACES"

This is particular handy for paths e.g. ${ProjectDir}Bin\$Config\Images. However, if there is a \ after the variable name, that is enough for PowerShell to consider that not part of the variable name.

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

2 Comments

How can I write this one: Write-Host "\\"$pc[$i]? I need 2 backslashes before a variable without spaces. This solution has one space between the second backslash and the var value.There should be a param like -nonewlilne for spaces.
"Possibly canonical" Possibly? Do you have a citation or is this just your preference/gut feeling?
29

You need to wrap the variable in $()

For example, Write-Host "$($MyVariable)NOSPACES"

4 Comments

In my testing this put a space between the variable and the string literal.
This is what I see: $MyVariable = "Some text" Write-Host "$($MyVariable)NOSPACES" Some textNOSPACES
What is wrong? imgur.com/a/eLmBtlg Check that. There is no space between the variable text and the string literal.
Ultimately, I found I was inserting spaces through improper syntax, but first I jumped on Keith's answer - and it didn't work. It works if $MyVariable contains a String, but not if it contains an Object with a Property storing the String; ${ObjectVariable.SomeProperty} resolves to nothing This answer works for Object Properties too.
14
Write-Host $MyVariable"NOSPACES"

Will work, although it looks very odd... I'd go for:

Write-Host ("{0}NOSPACES" -f $MyVariable)

But that's just me...

Comments

5

You can also use a back tick ` as below:

Write-Host "$MyVariable`NOSPACES"

2 Comments

This variant has an advantage that it works also within a command executed from the script (unlike ${MyVariable} and $($MyVariable))
This variant also has the disadvantage that if the character following the backtick matches a special character, it will insert that special character (try "$MyVariable`nospaces", for example).
3
$Variable1 ='www.google.co.in/'

$Variable2 ='Images'

Write-Output ($Variable1+$Variable2)

1 Comment

Further to the question, this is another simple way to concatenate 2 variables without space. Putting both variables as shown in the above example, the output will be "www.google.co.in/images"
1

Easiest solution: Write-Host $MyVariable"NOSPACES"

4 Comments

It works, but PowerShell will not let you insert multiple spaces between the variable and the string literal.
Why wouldn't you just move the spaces to the string literal? Write-Host $MyVariable" NOSPACES"?
$MyVariable = "x"... $MyVariable"NOSPACES" --> x NOSPACES. <-- inserts a space. This works --> ($MyVariable"NOSPACES")`
This repeats one of the solutions from stackoverflow.com/questions/5144110/… from a year prior.
0

if speed matters...

$MyVariable = "Some text"

# slow:
(measure-command {foreach ($i in 1..1MB) {
    $x = "$($MyVariable)NOSPACE"
}}).TotalMilliseconds

# faster:
(measure-command {foreach ($i in 1..1MB) {
    $x = "$MyVariable`NOSPACE"
}}).TotalMilliseconds

# even faster:
(measure-command {foreach ($i in 1..1MB) {
    $x = [string]::Concat($MyVariable, "NOSPACE")
}}).TotalMilliseconds

# fastest:
(measure-command {foreach ($i in 1..1MB) {
    $x = $MyVariable + "NOSPACE"
}}).TotalMilliseconds

1 Comment

This doesn't provide any new ways from the existing answers, it just provides code to benchmark the existing answers. Please only provide answers that answer the question that was asked. If you want to provide benchmarking info on existing answers, do so via a comment to that answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.