Whatever you call it, I'm trying to figure out a way to get the contents of an existing string and evaluate them as a double-quoted string. For example, if I create the following lines:
$string = 'The $animal says "meow"' $animal = 'cat'
Then Write-Host $string produce The $animal says "meow" . How can I override $ string, print (or assign to a new variable) The cat says "meow" ?
How annoying ... restrictions on comments make it difficult (if possible) to include code with inverse outputs. Here is an unreleased version of the last two comments I made in response to zdan below:
----------
Actually, thinking about this, I realized that you should not expect that The $animal says "meow" will interpolate without avoiding double quotes, because if it were a double-quoted string, the rating would be violated if double quotes did not run away. Therefore, I assume that the answer will be a two-step process:
$newstring = $string -replace '"', '`"' iex "`"$string`""
One last comment for posterity: I experimented with ways to get everything on one line, and almost everything that you think works as soon as you feed it, i.e. iex, but this works:
iex ('"' + ($string -replace '"', '`"') + '"')
source share