none
Usage of $ inside PS variables RRS feed

  • Question

  • Hello.

    Wasn't able to use string with $ inside. This string comes from external source and I need just pass it thru, but instead PowerShell tries to interpret it as a variable.

    Like $var ="someting $(here)"

    Thank you in advance!

    Tuesday, May 28, 2019 11:04 AM

Answers

  • The PowerShell escape character is the backtick ,"`", (also called the back apostrophe). It causes the escaped character to be taken literally. Replace any "$" characters with "`$", and the string will not be interpreted as a variable.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Tuesday, May 28, 2019 1:06 PM
    Moderator
  • As Richard said, you can 'escape' the $ character in a string using the backtick `.

    You can also use string formatting (-f operator) and single quotes. Example:


    $s = 'Something {0}' -f '$here'
    # $s variable now contains the string 'Something $here'
    


    -- Bill Stewart [Bill_Stewart]

    Tuesday, May 28, 2019 1:55 PM
    Moderator

All replies

  • The PowerShell escape character is the backtick ,"`", (also called the back apostrophe). It causes the escaped character to be taken literally. Replace any "$" characters with "`$", and the string will not be interpreted as a variable.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Tuesday, May 28, 2019 1:06 PM
    Moderator
  • As Richard said, you can 'escape' the $ character in a string using the backtick `.

    You can also use string formatting (-f operator) and single quotes. Example:


    $s = 'Something {0}' -f '$here'
    # $s variable now contains the string 'Something $here'
    


    -- Bill Stewart [Bill_Stewart]

    Tuesday, May 28, 2019 1:55 PM
    Moderator