'$ quick question

This topic contains 4 replies, has 2 voices, and was last updated by  turbo mcp 3 years, 9 months ago.

  • Author
  • #19765

    turbo mcp

    I have a script that's supposed to change a value in a file on multiple servers
    it works and all, but as I would like to understand and learn what each part does(99.9 % i do)
    I cannot understand what this little $ sign does and why do I need it:
    -replace 'MaxActiveMovesPerSourceMDB = "5"$','MaxActiveMovesPerSourceMDB = "50"' `

    why do I need the $ sign before I close the value i want to replace with a ' ?

    replace 'MaxActiveMovesPerSourceMDB = "5"$',


  • #19767

    Dave Wyatt

    PowerShell's -replace operator uses regular expressions for its first argument. In a regex, the $ character means to only match the pattern at the end of the string (or at the end of a line, depending on how you're using it.) There's an about_Regular_Expressions help file in PowerShell that can help you get started with that.

  • #19769

    turbo mcp

    Thanks for a blazing fast reply:)
    I only saw one documentation of this '$ sign but I wasn't sure if it even fits what I see here cause I got some value enclosed with 'value1' to replace with another value 'value2'
    in my case I think im replacing/matching whatever is inside these ' ' so that's why I don't understand why this $ is needed:)
    but maybe its just faster this way it match "end characters instead of the whole value?
    I wonder what "end character mean anyway:)? 1?3? or just from the end to start whatever matches?
    sorry for the many questions

  • #19772

    Dave Wyatt

    Well, say that your string varialbe contained this:

    $string = "cat dog cat dog cat dog"

    You can see the difference between using the $ in the pattern or not:

    $string -replace 'dog', 'zebra' # This replaces all 3 instances of "dog"
    $string -replace 'dog$', 'zebra'  # This replaces only the word "dog" at the very end of the string.
  • #19778

    turbo mcp

    Thanks a million

You must be logged in to reply to this topic.