This topic contains 5 replies, has 4 voices, and was last updated by

3 years, 11 months ago.

  • Author
  • #24726

    Points: 0
    Rank: Member

    Is it possible to use a backtick to make my text readable in the following statement. My liline for the replace will be long.


    Is it possible to have a statement like this

    $_.Replace('Host="Detroit1"',' Host="Log123#" ') `
    .Replace('Host="Devnver12"',' Host="Files132" ')`
    .Replace('Host="Devnver12"',' Host="Files132" ')

    I I am getting the message below.

    + $_.Replace('Host="Detroit1"',' Host="Log123#" ') '
    + ~
    Unexpected token "


  • #24727

    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    It's legal, but it is not legal to have a space between the closing parentheses ) and the period . that comes before the next method name.

  • #24728

    Points: 0
    Rank: Member

    You can also do this without backticks if you put the dot operator at the end of the line. That's one of the freebie line continuation characters that PowerShell gives you, because it knows that it's not a complete expression without something to the right of the dot:

    $string = 'I am a string'
    $string.Replace('string', 'frog').
            Replace('am', 'found')
  • #24735

    Points: 1
    Rank: Member

    Also, you can break on the parens as if they were a block:

    "Hello, there!".
  • #24736

    Points: 1
    Rank: Member

    And since you are chaining the replaces, seems like a good opportunity to loop over sets of replacement terms defined in a hash table. It'll be a lot easier to read and troubleshoot as the number of your replacement terms increases.

    $r = @{
        "there" = "Mikheal"
        "Hello" = "Goodbye"
    $str = "Hello, there!"
    ForEach($key in $r.Keys) {
        $str = $str -replace $key, $r[$key]
  • #24738

    Points: 0
    Rank: Member

    Thank you all (Don, Dave & Craig) for your input. This has given a better insight.

The topic ‘Backtick’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort