3

I am trying to concat variable in the string.

Code :

$uniqueID = "123"
$Key = '<add key="uniqueID" value="$uniqueID" />'
write-host $Key

Result i want :

<add key="uniqueID" value="123" />

Result i am getting :

<add key="uniqueID" value="$uniqueID" />
1
  • 1
    $Key = "<add key='uniqueID' value='$uniqueID' />". Better yet, don't build your XML by string concatenation. PowerShell comes with an XML parser. Use it. Commented Jul 9, 2018 at 18:51

2 Answers 2

6

Try this -

$Key = "<add key=`"uniqueID`" value=`"$($uniqueID)`" />"

OR

$Key = "<add key=`"uniqueID`" value=`"$uniqueID`" />"

To force Windows PowerShell to interpret a double quotation mark literally, use a backtick character. This prevents Windows PowerShell from interpreting the quotation mark as a string delimiter.

INFO -

If you look at Get-Help about_Quoting_Rules, it says,

SINGLE AND DOUBLE-QUOTED STRINGS
   When you enclose a string in double quotation marks (a double-quoted
   string), variable names that are preceded by a dollar sign ($) are
   replaced with the variable's value before the string is passed to the
   command for processing.

   For example:

       $i = 5
       "The value of $i is $i."

   The output of this command is:
       The value of 5 is 5.

   Also, in a double-quoted string, expressions are evaluated, and the
   result is inserted in the string. For example:

       "The value of $(2+3) is 5."

   The output of this command is:

       The value of 5 is 5.

   When you enclose a string in single-quotation marks (a single-quoted
   string), the string is passed to the command exactly as you type it.
   No substitution is performed. For example:

       $i = 5
       'The value of $i is $i.'

   The output of this command is:

       The value $i is $i.

   Similarly, expressions in single-quoted strings are not evaluated. They
   are interpreted as literals. For example:

       'The value of $(2+3) is 5.'

   The output of this command is:

       The value of $(2+3) is 5.

The reason your code wasn't evaluating the value was because you were using a single quote to wrap the variable $key. Wrapping it with double quote and using a sub-expression operator ($($UniqueID) along with backtick does the trick, or simply using the $UniqueID will also suffice.

Sign up to request clarification or add additional context in comments.

3 Comments

This will give me : <add key='uniqueID' value='123' /> but i need double quotes instead of single.
You can use a backtick to treat double quotes as literal values. Check out the solution now.
Note that you can also use ""quoted text"" without using a backtick as well (gets rendered as "quoted text"). this also works in single quoted strings for single quotes.
1

I find using the '-f' formatting option works great for this kind of request. Try:

$uniqueID = "123"
$Key = ('<add key="{0}" value="{1}" />' -f 'uniqueID', $uniqueID)
write-host $Key

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.