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
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.
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.You need to wrap the variable in $()
For example, Write-Host "$($MyVariable)NOSPACES"
$MyVariable = "Some text" Write-Host "$($MyVariable)NOSPACES" Some textNOSPACESYou can also use a back tick ` as below:
Write-Host "$MyVariable`NOSPACES"
${MyVariable} and $($MyVariable))"$MyVariable`nospaces", for example).$Variable1 ='www.google.co.in/'
$Variable2 ='Images'
Write-Output ($Variable1+$Variable2)
Easiest solution: Write-Host $MyVariable"NOSPACES"
Write-Host $MyVariable" NOSPACES"?$MyVariable = "x"... $MyVariable"NOSPACES" --> x NOSPACES. <-- inserts a space. This works --> ($MyVariable"NOSPACES")`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
$MyVariableN(or another collision) that PS would figure out what you meant. No dice.