none
Concatenate strings and variables

    Question

  • simple concatenation

    $a = "ThisIsA"

    echo "=======" + $a + "======="

    gives me

    +

    ========

    +

    ThisIsA

    I want    ========ThisIsA========

    I tried several forms of the join operator.  I tried taking out the +.  I tried replacing

    the + with commas.    They all give me pretty much the same thing:  at least 3 lines of output.

    I'm looking for one.     This has got to be easy.  I suppose it could be that I have been testing with v1.0 rather than 2.0, but I suspect not.

    Solutions would be much appreciated.  I've searched the heck out of the internet with no success.

    Tuesday, April 24, 2012 2:28 AM

Answers

  • Also,
     
    "=======" + $a + "=======" | echo
     On 4/24/2012 11:33 AM, Larry Weiss wrote:
    > $a = "ThisIsA"
    > echo $("=======" + $a + "=======")
    > will do what you expect.
     
     
    • Marked as answer by dbthj Tuesday, April 24, 2012 8:27 PM
    Tuesday, April 24, 2012 4:59 PM

All replies

  • You are not in the DOS world anymore.

    $a = "ThisIsA" "=======" + $a + "=======" or "=======$a=======" or "="*7 + $a + "="*7
    or
    "======{0}=======" -f $a

    #if you want some color you can go with write-host -fore Red "=======$a======="



    Cyreli


    • Edited by Cyreli Tuesday, April 24, 2012 3:53 AM
    • Proposed as answer by Kaplaa Friday, March 01, 2013 7:43 PM
    Tuesday, April 24, 2012 3:52 AM
  •  
    take a look at that, it goes over working with strings
     

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Tuesday, April 24, 2012 12:31 PM
  • Silly me for thinking a verb would help get something done.

    Thanks.  

    Just out of curiosity, though,  Why does echo or join put all the stuff on different lines?   Is there no concatenation operator that would work with a verb?

    I'm  a newbie.  Just spent 3 days in a Microsoft "essentials"  powershell class and I'm trying to start making some sense out of it.

    Tuesday, April 24, 2012 4:13 PM
  • echo is an alias for write-output which deals in objects, and writes the
    value of those objects
     
    it’s the difference between
     
    write-host hello world
    write-output hello world
     
    collection of objects vs a string
    also you'll notice that the " are needed because it forces it to string...
    honestly, it’s a little odd and im not sure why they do that or what magic
    is making that happen.
     
    join is an operation on strings so it takes the array and joins them with
    whatever you want, so in the examples above you can use the write-output (or
    echo) because it gives a collection in return.
     
    (write-output hello world) -join "."
     
    you have to use the () around write-output because it eats up everything it
    can..
     
     

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Tuesday, April 24, 2012 4:20 PM
  • $a = "ThisIsA"
    echo $("=======" + $a + "=======")
     
    will do what you expect.
     
    Tuesday, April 24, 2012 4:33 PM
  • echo is an alias for Write-Output
     
    Write-Output takes an object or an object array as the first argument and
    also has provision for accepting "extra" arguments for the "Object" parameter:
     
    C:> (get-command write-output).Parametersets
     
    Parameter Set Name: __AllParameterSets
    Is default parameter set: False
       Parameter Name: InputObject
        ParameterType = System.Management.Automation.PSObject[]
        Position = 0
        IsMandatory = True
        IsDynamic = False
        HelpMessage =
        ValueFromPipeline = True
        ValueFromPipelineByPropertyName = False
        ValueFromRemainingArguments = True        <------------------
        Aliases = {}
        Attributes =
          System.Management.Automation.AllowEmptyCollectionAttribute
          System.Management.Automation.AllowNullAttribute
          System.Management.Automation.ParameterAttribute
     
    The command line is not parsed as a total expression so each component that forms
    a token is taken as a new element in the InputObject array to Write-Output.
     
    In
    echo "=======" + $a + "======="
    the + symbol is not applied as an operator but is taken
    as the first "Remaining" argument to be added as the second element
    of the input array.  And likewise for the other items on the command line.
     

    • Edited by Larry Weiss Tuesday, April 24, 2012 5:01 PM
    Tuesday, April 24, 2012 4:58 PM
  • Also,
     
    "=======" + $a + "=======" | echo
     On 4/24/2012 11:33 AM, Larry Weiss wrote:
    > $a = "ThisIsA"
    > echo $("=======" + $a + "=======")
    > will do what you expect.
     
     
    • Marked as answer by dbthj Tuesday, April 24, 2012 8:27 PM
    Tuesday, April 24, 2012 4:59 PM
  • very cool larry, thanks
     

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Tuesday, April 24, 2012 5:16 PM
  • The first time I really paid attention to the
     
    ValueFromRemainingArguments = True
     
    value of a Parameter set was for the -Process argument of
    ForEach-Object
     
    that helps make sense of
     
    C:> 1,2,3 | ForEach-Object {'begin'} {'process'} {'end'}
    begin
    process
    process
    process
    end
     
    Tuesday, April 24, 2012 6:00 PM
  • Thanks for the education.

    Maybe I'm too old for this object-oriented stuff, but I'll keep at it.

    Can you help me understand the solution mentioned of:

    "======{0}=======" -f $a

    It's not apparrent to me what's going on here.  Since there is no command explicitly involved I wouldn't know how to even look it up.... or what the -f  applies to.  It works, but I sure don't know why.

    Tuesday, April 24, 2012 8:27 PM
  • $a = "ThisIsA"
    "======{0}======="-f $a
     
    The -f is an operator that takes a formatting pattern on the left and values to be formatted on the
    right (if more than one value they are in an array).
     
    In this case the formatting pattern will embed the value of the first value on the right,
    at index 0 (hence the {0}) into the output surrounded by the runs of literal = characters.
     
    Since there is no command, the result is simply output to the PowerShell host (probably the screen).
     
    There is also ability to left or right justify a value into a fixed width field if padding is needed:
     
    C:> "======{0,-10}=======" -f $a  # left-justify
    ======ThisIsA   =======
    C:> "======{0,10}=======" -f $a   # right-justify
    ======   ThisIsA=======
     
    More info about -f is at
     On 4/24/2012 3:27 PM, dbthj wrote:
    > Can you help me understand the solution mentioned of:
    > "======{0}======="-f $a
    >
     
    Tuesday, April 24, 2012 9:23 PM
  • list the items seperating them with a comma ',' and use the - join operator:
    Syntax: <item-1 [, <item-2>]...> - join '<filler>'

    eg. 'x', 'y', 'z', join ''
    => xyz


    Monday, January 07, 2013 10:58 PM
  • You have to use write-host command instead of echo


    LFF

    Friday, June 28, 2013 8:36 PM
  • Consider the code below (with <##>  as the command prompt).  

    echo works fine.

    <##> $a = "ThisIsA"; "=======" + $a + "=======" | echo
    =======ThisIsA=======
    <##>

    Friday, June 28, 2013 10:19 PM
  • thanks - that is what I was looking for
    Wednesday, September 10, 2014 3:48 PM
  • The crux of this particular issue seems to be with Powershell's dual personalities, called parsing modes:  Command and Expression.  I found this to be a helpful tutorial to get them clear:  https://rkeithhill.wordpress.com/2007/11/24/effective-powershell-item-10-understanding-powershell-parsing-modes/

    Thursday, February 18, 2016 7:18 AM